Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Widget.onLoad() isn't called???

    Does Widget.onLoad() really not get called?
    If so, what should I use to trigger logic that should happen after a Layout and its contents have been rendered and attached to the DOM?

    I want to load data from a ds manually whenever a particular Layout is displayed. By 'manually' I mean for example a ListGrid with settings setSaveLocally(true) & setAutoFetchData(false) which needs to have some other code get the data and load it to the grid. I would expect to do the data loading in an override of GWT's Widget.onLoad(). But my onLoad() doesn't ever get called.

    Here's some pseudocode of of my layout class....
    Code:
    public class MyLayout extends VLayout {
    
       public MyLayout() {
          // add forms and lists etc. that will get data loaded manually
       }
    
       @Override
       protected void onLoad() {
          super.onLoad();
          // Want to load data here, but ...
          // This method is never called, apparently.
       }
       
       @Override
       protected void onAttach() {
          super.onAttach();
          // This method is also never called, apparently.
       }
    }
    I'm confused because Layout ultimately extends Widget and so /should/ have its onAttach() and onLoad() methods called.

    help!
    ,chris

    #2
    Sorry. I meant this to be posted to the SmartGWT Technical Q&A forum.
    ,chris

    Comment


      #3
      There's no need for such an API because when you call draw() or add a widget to a drawn parent, it's drawn synchronously. But if you are trying to add some logic that fires on draw and your code won't actually be calling draw(), add a DrawHandler.

      Comment


        #4
        OK. I tried overriding onDraw() and got what I expected. ... Also, I'll rethink my design so that the thing that calls draw() might then call loadData(). Sounds like a good path.

        Nonetheless, some javadoc on BaseWidget about the non-supported GWT methods might be useful. ... It could even get draconian and make them final (oh, I hear the screams already!)

        Anyway, Thanks!
        chris

        Comment


          #5
          onDraw didn't work for me, but this did:
          Code:
          WindowMine win=new WindowMine();
          win.show();
          win.window_Shown();
          In other words, just explicitly call your own method instead of depending on an event.

          Comment


            #6
            is there an onAttach alternative?

            I'm sorry to bring this up after a while but I'm having a little problem.

            I'm trying to resize a CKEditor once the wrapping Layout is drawn/attached/loaded, so I tried this

            Code:
            @Override
            protected void onDraw() {
            	super.onDraw();
            	// after is drawn we could get height of component
            	log.info("Resizing editor: 100%, " + getHeightAsString());
            	resizeEditor(ckeEditor, "100%", getHeightAsString());
            }
            but it isn't working because the Layout html element (and therefore its children) hasn't been attached to the DOM (I suppose).

            I worked around this issue with a Timer

            Code:
            @Override
            protected void onDraw() {
            	super.onDraw();
            	Timer timer = new Timer() {
            		@Override
            		public void run() {
            			// after is drawn we could get height of component
            			log.info("Resizing editor: 100%, " + getHeightAsString());
            			resizeEditor(ckeEditor, "100%", getHeightAsString());
            		}
            	};
            	timer.schedule(1 * 1000);
            }
            but I think it's not the best way to do it. What would happen if the browser takes more than 1s in attaching the Layout?

            I'd really appreciate any hint on this...

            Comment


              #7
              I had the same problem as you and my solution is to check periodically if the CKEditor is already attached to the DOM or not, and then call the native functions.

              Example:

              Code:
              public void setData(final String text) {
              	if (attachedToDOM) {
              	    setDataNative(text);
              	} else {
              	    AsyncCallback callback = new AsyncCallback() {
              
              		@Override
              		public void callback(Response response, String errorMessage,
              			Object responseObject) {
              		    setDataNative(text);
              		}
              	    };
              	    periodicallyCheckIfAttachedAndExecute(callback);
              	}
                  }
              
              private void periodicallyCheckIfAttachedAndExecute(
              	    final AsyncCallback callback) {
              	final Timer timer = new Timer() {
              
              	    @Override
              	    public void run() {
              		System.out.println("Attached: " + attachedToDOM);
              		if (attachedToDOM) {
              		    System.out.println("Attached: " + attachedToDOM
              			    + ", so cancelling timer.");
              		    cancel();
              		    callback.callback(Response.OK, null, null);
              		}
              	    }
              
              	};
              	timer.scheduleRepeating(50);
                  }
              
              public native void setDataNative(String text) /*-{
              		this.@zedes2.client.ui.widgets.TCKEditor::ckEditor.setData(text);
                  }-*/;
              so it periodically checks if the variable "attachedToDOM" is set and THEN it calls the native function.

              The "attachedToDOM" variable is set here:

              Code:
              @Override
                  protected native void onDraw() /*-{
              		this.@zedes2.client.ui.widgets.TCKEditor::ckEditor = $wnd.CKEDITOR
              				.replace(this.@zedes2.client.ui.widgets.TCKEditor::getID()()
              						+ "_ckEditor", {
              					// Use a base zIndex of 300,000 for the dialogs such as the Insert Special Character dialog.
              					baseFloatZIndex : 300000,
              
              					// Disable these plugins because they cause the CKEditor widget to grow outside of the
              					// canvas' bounds.
              					removePlugins : "autogrow,resize,maximize,div,elementspath"
              				});
              
              		//this.@zedes2.client.ui.widgets.TCKEditor::ckEditor.setData("test");
              		this.@zedes2.client.ui.widgets.TCKEditor::attachedToDOM = true;
                  }-*/;

              Comment

              Working...
              X