1
votes

I'd like to pass a function parameter (i.e. a FunctionObject) in SSJS to a bean, and then call that function as a kind of callback function. For instance:

companyBox.setGenerator(function() {
    return @DbColumn("", SystemBean.getViewName(), 2);              
});

so that I can call that function whenever I need actualized values.

I'm expecting this to fail because the SSJS-context is missing, but the idea is still tempting. Or maybe it's because the function cannot be Serialized, but what that exactly means I don't know (yet). In this case, the companyBox object is created from a managed bean with view/page scope: once the current page disappears I no longer need it.

Clues are welcome...

1
I don't know what is possible... With all respect I don't know why you would even want to do this. If you're working with Java - then move to java and be done with it. Trying to mix it in with @functions just seems off to me when you could just do all the work in Java itself.David Leedy
@David: it's the management module that I cannot change (yet), and it's in SSJS. The bean is a multi-purpose class, and it requires a function that generates selectable values, e.g. for a drop-down list. All I'd like to do is call that function. I could indeed pass its name, but I just asked myself: wouldn't it be better to pass the function ans an object, so it can be called (back)?D.Bugger
@Thomas: Thanks for the link, I saw that question and... I forgot about it. It might be doable that way, but then I'd have to pass the name (or the whole function) as a string. Not ideal, but if it's the only way, I'll probably do that.D.Bugger
Hm, I expect I can use a formula-string in Formula language in 95% of all cases. That case seems closed, but I'd still be interested to know whether calling a function using a FunctionObject from Java is possible.D.Bugger

1 Answers

1
votes

Yes it is. You have two options.

1) Use "value binding". Thomas Adrian commented about this question, Sven Hasselbach blogged about calling external SSJS library (cool stuff, by the way).

In short: make a call to inner JSF engine to resolve value binding - what can be SSJS (including call to your method) or any other binding (EL). And it works with "on fly" constructed expressions, passed as String.

2) Use Function object as parameter. Blueprint for that technique is available here. Your comment reveals you are aware of com.ibm.jscript package. Well, current implementation of call() method of com.ibm.jscript.std.FunctionObject class will support your empirical observation:

/*     */   public FBSObject call(FBSValueVector paramFBSValueVector) {
/* 163 */     return null;
/*     */   }

Hint: JD Eclipse and JD Eclipse-realign strongly recommended!

The other call method with more complex signature is the way to go. It is used to define custom @Functions, for example this snippet.