cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript & Java classes

dozyarmadillo
Champ on-the-rise
Champ on-the-rise
Hi there

I have some code that parses an Excel spreadsheet. There is a parse() method on my service that takes a NodeRef (which would point to the uploaded spreadsheet). It returns an object graph of POJOs. The method signature looks like this:

MyConfiguration parse(NodeRef workbook) throws MyException;

I wrapped this code in an Action and this works fine. I would now like to expose this same functionality via server side JS. I extended BaseProcessorExtension and registered the extension via Spring.

I have the following JS code:


myextn.parse(document);

This works. I see a bunch of debug logging in the logfile that confirms that the spreadsheet has been parsed and the values transposed into a MyConfiguration object.

Now if I try this..


var cfg = myextn.parse(document);
// should execute the method MyService.getSomeProperty()
logger.log(cfg.someProperty);

..this fails. The variable cfg is reported as being null even though I can see that the spreadsheet was parsed OK via logging.

Are there any restrictions re exposing custom Java objects in JS that I should be aware of?

Thanks

Mark
2 REPLIES 2

mrogers
Star Contributor
Star Contributor
Yes there are some fairly stringent limitations.

The first is how "MyConfiguration" will be exposed to JavaScript.   From memory, some very basic collections will be mapped, otherwise it will require some custom mapping.  

When I'm witing a java script extension, I tend to add a "script" object that converts from a Java object to a script object.   It can also hide any bits that can't work with script or add helper methods if required.

dozyarmadillo
Champ on-the-rise
Champ on-the-rise
Thanks for that. I ended up adding a new Script object (implementing Scopeable) and as I was doing this found a silly bug in my JS extension that was the cause for the NPE. It's always the obvious error.. 🙂