How to pass variable parameters to an XPages SSJS function?

北战南征 提交于 2020-01-03 01:53:06

问题


If I have a function in SSJS and I want to pass one "firm" parameter and a list of others that can change, what's the best way to do that? With some kind of hashMap or JSON or something else?

for example given something like:

myfunction( code:string, paramList:??) { // do stuff here

}

Basically the function will create a document. And sometimes I'll have certain fields I'll want to pass in right away and populate and other times I'll have different fields I will want to populate.

How would you pass them in and then parse out in the function?

Thanks!


回答1:


Use the arguments parameter... In JavaScript you are not required to define any of your parameters in the function block itself. So, for example, the following call:

myFunction(arg1, arg2, arg3, arg4);

can legally be passed to the following function:

myFunction () {
  // do stuff here...
}

when I do this, I usually place a comment in the parens to indicate I am expecting variable arguments:

myFunction (/* I am expecting variable arguments to be passed here */) {
  // do stuff here...
}

Then, you can access those arguments like this:

myFunction (/* I am expecting variable arguments to be passed here */) {
  if (arguments.length == 0) {
    // naughty naughty, you were supposed to send me things...
    return null;
  }

  myExpectedFirstArgument = arguments[0];

  // maybe do something here with myExpectedFirstArgument
  var whatEvah:String = myExpectedFirstArgument + ":  "

  for (i=1;i<arguments.length;i++) {
    // now do something with the rest of the arguments, one 
    // at a time using arguments[i]
    whatEvah = whatEvah + " and " + arguments[i];
  }

  // peace.
  return whatEvah;
}

Wallah, variable arguments.

But, more to the point of your question, I don't think you need to actually send variable arguments, nor go through the hassle of creating actual JSON (which is really a string interpretation of a javascript object), just create and send the actual object then reference as an associative array to get your field names and field values:

var x = {};
x.fieldName1 = value1;
x.fieldName2 = value2;
// ... etc ...

then in your function, which now needs only two parameters:

myFunction(arg1, arg2) {
   // do whatever with arg1

   for (name in arg2) {
     // name is now "fieldName1" or "fieldName2"
     alert(name + ": " + x[name]);
   }

}

Hope this helps.




回答2:


I would do this with a JSON object as the second parameter...

function myfunction(code:String, data) {
   // do stuff here...
   var doc:NotesDocument = database.CreateDocument();
   if(data) {
      for (x in data) {
         doc.replaceItemValue(x, data[x]);
      }
   }
   // do more stuff
   doc.save(true, false);
}

Then you call the function like this:

nyfunction("somecode", {form:"SomeForm", subject:"Whatever",uname:@UserName()});

Happy coding.

/Newbs




回答3:


I don't think that is possible in SSJS. I think the best option you have is to pass a hashmap or your own (java) object. I think a custom java object would be the nicest option because you can define some 'structure' on how your function can process it. A hashmap can be easily extended but it is not easy if you have a lot of code that create a lot of different hashmap structures...



来源:https://stackoverflow.com/questions/9897636/how-to-pass-variable-parameters-to-an-xpages-ssjs-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!