dominoGuru.com
Your Development & Design Resource
Using conditional arguments in SSJS functions for XPages
06/10/2011 12:34 AM by Chris Toohey
In JavaScript, arguments
is a reserved word variable which
contains an array of the arguments passed to the given function.
You can use the arguments
to conditionally check for function
arguments in JavaScript -- and thus Server-side JavaScript.
Here's a simple abstracted example of an @Post
SSJS function
that I'm using for an XPages MVC architecture framework that I'm writing:
var @Post=function() {
var xDoc;
if (arguments[0] !=
undefined && arguments[0] != null) {
if (currentDocument ==
undefined) {
xDoc =
database.createDocument();
} else {
xDoc =
currentDocument;
}
} else
{
xDoc = arguments[0];
}
...
xDoc.Save();
if (arguments[1] !=
undefined) {
context.redirectToPage(arguments[1]);
}
}
This function should be pretty simple to follow. Let's say I wanted to simply submit the currentDocument from within a NotesDocument-DataSource'd XPage:
@Post()
In another case, I might want to call this from a dataTable Control (where rowData is the control variable and said dataTable is bound to a NotesDocumentCollection) to update a given NotesDocument from a view:
@Post(rowData)
You can even pass a null for the first argument, saving either the currentDocument or creating a new NotesDocument, and finally redirecting the user back to the index.xsp XPage post-save:
@Post(null,
"index.xsp")
Or, I dunno, update the parent NotesDocument of a given NotesDocument from a view dataTable Control:
@Post(database.getDocumentByUNID(rowData.getParentDocumentUNID()))
Of course, this is just a simple example. Once you get the idea behind using
arguments
, you can do some really slick stuff... like feed a
function a list of UNIDs:
var @ApproveRequest = function() {
var xDoc:NotesDocument;
for( var i = 0; i <
arguments.length; i++ ) {
xDoc = database.getDocumentByUNID(arguments[i]);
xDoc.replaceItemValue("status", "Approved");
@Post(xDoc);
}
}
Now, of course I'd use an Array object in JavaScript to store those UNIDs... but you get the idea. Pretty slick stuff once you get your head around it.
Have a better example use of conditional arguments? Share them below in the comments section!