简体   繁体   English

在@PostConstruct中调用JSNI方法-Errai

[英]Calling JSNI method in @PostConstruct - Errai

I'm having an issue trying to call a JS function from Java using Errai 2.3.2 I'm using Bootstrap switch and I need to invoke a JS function in order to activate it. 我在尝试使用Errai 2.3.2从Java调用JS函数时遇到问题,正在使用Bootstrap开关,我需要调用JS函数才能激活它。 I need to do this after the page is created and all the elements are attached to the DOM element. 创建页面并将所有元素都附加到DOM元素后,我需要执行此操作。

I've tried it all: @PostConstruct, @PageShowing, @PageShown, onAttach().. but none of those worked for me. 我已经尝试了所有方法:@ PostConstruct,@ PageShowing,@ PageShown,onAttach()..但是这些都不对我有用。

I have this: 我有这个:

@Override
public void onAttach(){
    enableToggleSwitch();
}

public static native void enableToggleSwitch() /*-{
    $wnd.enableToggle();
}-*/;

and when I debug it, it seems to be that the onAttach() method is invoked before the elements are attached to the DOM. 当我调试它时,似乎是在将元素附加到DOM之前调用了onAttach()方法。

Any ideas? 有任何想法吗?

You should not override the onAttach() method of the Widget class. 您不应覆盖Widget类的onAttach()方法。 If you want to do something in case a widget is attached, override the onLoad-method. 如果要在附加了窗口小部件的情况下执行某些操作,请覆盖onLoad方法。

  /**
   * This method is called immediately after a widget becomes attached to the
   * browser's document.
   */
  protected void onLoad() {
      enableToggleSwitch();
  }

Try this. 尝试这个。

If this does not solve your problem, you can try to use a Scheduler: 如果这不能解决您的问题,则可以尝试使用调度程序:

    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        @Override
        public void execute() {
            enableToggleSwitch();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM