0

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. I need to do this after the page is created and all the elements are attached to the DOM element.

I've tried it all: @PostConstruct, @PageShowing, @PageShown, onAttach().. but none of those worked for me.

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.

Any ideas?

cristianmiranda
  • 130
  • 2
  • 11

1 Answers1

1

You should not override the onAttach() method of the Widget class. If you want to do something in case a widget is attached, override the onLoad-method.

  /**
   * 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();
        }
    });
El Hoss
  • 3,767
  • 2
  • 18
  • 24
  • Thanks! I've used the Scheduler inside the onAttach() method. It works! – cristianmiranda Nov 18 '13 at 18:54
  • @el-hoss why "Should not override the onAttach()" any specific reason :-) – quarks Feb 22 '14 at 20:29
  • onLoad() is called after all Widgets are attached and the attached flag is set. Take a look at the Widget.java. That's the right place, to do something when your widget is attached. – El Hoss Feb 22 '14 at 22:40