-1

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
EternallyCurious
  • 2,345
  • 7
  • 47
  • 78

2 Answers2

2

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()

}
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 2
    As @Baadshah says it is not necessary the `onReady()` method in gQuery. That is so, because the asynchronous nature of gwt loading process. Think that in jQuery it is needed because you write code inside the html, and it is executed before the document full loads, so you must delay it until the dom available. But in GWT we include a `.nocache.js` file which will load the appropriate `.cache.js` permutation asynchronously, so it will run after the document loads. As curiosity the exported version of gQuery [jsQuery](http://code.google.com/p/gwtquery/wiki/JsQuery) implements it. – Manolo Carrasco Moñino Apr 13 '13 at 11:23
2

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.

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27