【问题标题】:How can I pass an anonymous JavaScript object from Java to JavaScript in GWT?如何将匿名 JavaScript 对象从 Java 传递到 GWT 中的 JavaScript?
【发布时间】:2009-09-21 23:31:16
【问题描述】:

我正在围绕一个 JavaScript 库创建一个 GWT 包装器。其中一个 JavaScript 函数将匿名对象作为其参数,例如:

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

在 Java 端,我如何创建这种类型的 JavaScript 对象并将其传递给我的本机实现?

目前,在 Java 方面我有:

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

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

任何指针表示赞赏,谢谢。

【问题讨论】:

    标签: java javascript gwt jsni


    【解决方案1】:

    如果您确切知道应该使用哪些参数,您可以执行以下操作(删除 :: 之后的额外新行)

    private native void buildTabs(TabConfiguration config) /*-{
            $wnd.NAMESPACE.lib.buildTabs({hide: 
                    config.@com.yournamehere.TabConfiguration::
                    getHide()(), 
                    placeholder: 
                    config.@com.yournamehere.TabConfiguration::
                    getPlaceholder()()});
    }-*/;
    

    来自GWT documentation的小片段:

    public native void bar(JSNIExample x, String s) /*-{
        // Call instance method instanceFoo() on this
        this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
    
        // Call instance method instanceFoo() on x
        x.@com.google.gwt.examples.JSNIExample::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 = this.@com.google.gwt.examples.JSNIExample::myInstanceField;
    
        // Write instance field on x
        x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";
    
        // Read static field (no qualifier)
        @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
      }-*/;
    

    【讨论】:

      猜你喜欢
      • 2012-03-29
      • 2017-10-11
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 2012-03-18
      • 2014-10-07
      相关资源
      最近更新 更多