views:

598

answers:

2

Hi,

I'm creating a GWT wrapper round a JavaScript library. One of the JavaScript functions takes an anonymous object as its argument e.g.:

obj.buildTabs({ hide: true, placeholder: 'placeholder' });

On the Java side how do I create this type of JavaScript object and pass it to my native implementation?

At the moment, on the Java side I have:

public void buildTabs(TabConfiguration config) {
   // ?
}

private native void buildTabs(?) /*-{
     $wnd.NAMESPACE.lib.buildTabs(?);
}-*/;

Any pointers appreciated, thanks.

+1  A: 

if you exactly know what parameters should be used, you can do the following (remove additional new lines after ::)

private native void buildTabs(TabConfiguration config) /*-{
        $wnd.NAMESPACE.lib.buildTabs({hide: 
                [email protected]::
                getHide()(), 
                placeholder: 
                [email protected]::
                getPlaceholder()()});
}-*/;

a small clip from the GWT documentation:

public native void bar(JSNIExample x, String s) /*-{
    // Call instance method instanceFoo() on this
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call instance method instanceFoo() on x
    [email protected]::instanceFoo(Ljava/lang/String;)(s);

    // Call static method staticFoo()
    @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);

    // Read instance field on this
    var val = [email protected]::myInstanceField;

    // Write instance field on x
    [email protected]::myInstanceField = val + " and stuff";

    // Read static field (no qualifier)
    @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
  }-*/;
kaboom
+1  A: 

Refer: http://code.google.com/eclipse/docs/gwt%5Fjsni.html It describes how you can pass parameters between JavaScript and Java world. Also check out Google Plugin for Eclipse, if you find yourself working a lot with JSNI

Ashwin Prabhu