Friday, October 29, 2010

Re: About async calls...

You don't need a Timer in this case.  Remember that javascript is asynchronous, but not threaded, so you don't have to deal with reentry of an executing code block.

The code could look like the following.  You have separate methods to initialize the UI and to populate the UI.  Populating the UI is a no-op until both the UI is initialized and the RPC request comes back.

private String role = null;
private boolean uiInitialized = false;

public void onModuleLoad() {
  myService.sendServerRequest(new AsyncCallback() {
    public void onSuccess(String role) {
      this.role = role;
      maybePopulateUi(); // no-op if UI isn't initialized.
    }
  });

  initializeEmptyUi(); // create widgets with no data.
  maybePopulateUi(); // no-op if RPC call hasn't returned.
}

private void initializeEmptyUi() {
  uiInitialized = true;

  // Add code to initialize an empty Ui.
}

private void maybePopulateUi() {
  // Still waiting for UI to initialize and RPC to return.
  if (!uiInitialized || role == null) {
    return;
  }

  // Add code to populate Ui.
}

Thanks,
John LaBanca
jlabanca@google.com


On Fri, Oct 29, 2010 at 8:21 AM, Subhrajyoti Moitra <subhrajyotim@gmail.com> wrote:
" When RPC request returns, populate the state of the app.  If RPC request returns before the app it initialized, hold it until after the app is initialized."-
John, how would u do this? Using a timer to monitor a "common-state-variable"?

Thanks,
Subhro.

On Fri, Oct 29, 2010 at 5:46 PM, John LaBanca <jlabanca@google.com> wrote:
Waiting for the role to return and creating the app in onSuccess() is a perfectly valid solution.  To do so, you would just move your Widget initialization code into onSuccess().

Alternatively, you can load the app first, then populate it when the RPC request returns.  It would be a little more complicated, but the app would appear more responsive to the user because it loads immediately.
1. Send RPC request
2. Synchronously create the app
3. When RPC request returns, populate the state of the app.  If RPC request returns before the app it initialized, hold it until after the app is initialized.

Thanks,
John LaBanca
jlabanca@google.com


On Fri, Oct 29, 2010 at 5:03 AM, Pablo G.F <blayhck@gmail.com> wrote:
Hello:

I´m building a webapp which, depending on the user, will show some
options on the menu or other ones. For doing this, I invoke a Single
Singed On method passing as arguments, the user and the application
and getting as return value a string identifying the "role" of the
user. As I´m using GWT, I can´t invoke the SSO method from the client
side, so I have to make a RPC to the server, which will invoke the
method and return the "role" to the client.
I do all of this before "start creating the widgets that will be
showed", but I face the problem of asyncronous calls.
I invoke the RPC, but the return value may come late (by late I mean
when it has already started "building the widgets and appearance of
the web application".
The only solution I see (and I don´t know if it´s correct or even
possible) is start the building part  in the body of "onSuccess(String
result)" method, of the callback method associated to the RPC call.

I don´t know if I explanied it right...but I hope so.

Any other idea?

Thanks

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment