2
votes

Yet another javascript integration question. I have no idea know how to return a value from my java method to use it in the javascript code. Can you help?

        {
            //My java method
            JSObject dataObj = (JSObject) ctx.get("{}");
            dataObj.set("length", new JSFunction() {
                @Override
                public void apply(JSObject self, Object[] args) {
                    /*
                        Here, say I want to return the string length.
                        return args[0].toString().length();
                    */
                }
            });
            ctx.set("window.java", dataObj);
        }
        {
            String fn = "{ uf : function(a){ var l = java.length(a);}}";
            JSObject obj = (JSObject) ctx.get(fn);
            Object result = obj.call("uf", new Object[] {"Hello"});
        }

Thanks a lot.

E

1

1 Answers

0
votes

From the JavaDocs for JSFunction:

If you require a return value to Javascript, you can do that by passing a callback function which is called by the JSFunction with some parameters.

That's not ideal but I'm not sure if there is a way around it as the data transfer is probably asynchronous.