How do you use the $(document).ready(function) in GWTQuery?

1.4k Views Asked by At

I have been coding a project with GWTQuery but I can't find a GWTQuery equivalent of $(document).ready(function).

I tried doing a:

$(new Function(){ /* Function comes here */ });

and although this does not produce a syntactical error, any code written inside it produces no results.

2

There are 2 best solutions below

1
Suresh Atta On BEST ANSWER

You need not to write any ready function.

As in the linked question, onModuleLoad() is effectively the same as the ready event. By default, onModuleLoad doesnt run until after all of the resources in the page have loaded.

if INW,you can directly start writing in onModuleLoad

And as shown in GWTQuery guide,we can start writing code in onModuleLoad.

public void onModuleLoad() {
  //Hide the text and set the width and append an h1 element
  $("#text").hide()

}
2
Manolo Carrasco Moñino On

The $(Function) constructor in gQuery has a different meaning, it is a trick to use the syntax $(this) inside Functions.

In the example below $(this) is a shortcut to $("#input") or $(element). Note that this points to the inner Function.

 // gwtQuery version
 $("#input").click(new Function(){public void f() {
      $(this).text('whatever');
 }});

As you can see, we do this to have a code very similar to jQuery, so as it is easier to port code from jQuery to gQuery. In the case below this points to the context where the click is executed: the input element.

 // jQuery version
 $("#input").click(function() {
      $(this).text('whatever');
 });

About onReady question see @Baadshah's answer and my comment.