4

I've used the JSNI before but I've never had to pass a function pointer as a parameter using it and I'm not sure how to do this. Any help is appreciated!

2 Answers 2

5

You should be able to pass a JavaScriptObject that represents a JavaScript function object. I don't think you can do anything with Java functions, though. So for example you could:

final native JavaScriptObjet myFuncCreator() /*-{
    return function (x, y) { return y - x; };
}-*/

final native int myFuncUser(JavaScriptObject funcObj, int a, int b) /*-{
    return funcObj(a,b);
}-*/

Admittedly, I didn't try this code but I believe it should work.

Sign up to request clarification or add additional context in comments.

Comments

2

Along the same lines as sinelaw's answer, here's a way to get a callback.

static final native JavaScriptObject createFunction(final Runnable runnable)
/*-{
    return function() {
        [email protected]::run()();
    }
}-*/

static final void registerOnClickCallback(Element element, final Runnable runnable) {
    JavaScriptObject callback = createFunction(runnable);
    _registerOnClickCallback(element, callback);
}

static final native void _registerOnClickCallback(Element element, JavaScriptObject callback)
/*-{
    element.onclick = callback;
}-*/

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.