Call function after UiApp is loaded (Javascript on

2019-05-19 08:09发布

问题:

I have a UiApp form that requires some spreadsheet data (slowish retrieval). What I'd like to achieve is load the view as usual and after it has been displayed do something like calling a server handler that gets the data and updates my view when it's done. Is that possible?

I looked around but I didn't find anything.

According to this, "function doGet(e) is the GWT equivalent of onLoad", but it's not in my case as I would like to dispay the page and only then do the slow part...

回答1:

Stumbled on this which we could then use to implement a pseudo-onLoad event.

The setValue function on checkboxes has an optional parameter to fire the valueChanged handler. So you can programmatically trigger the valueChanged handler, which allows you to return the app (load it initially) while the handler is being invoked simultaneously.

function doGet() {
  var app = UiApp.createApplication();

  var label = app.createLabel('Loading the data from the spreadsheet.')
                 .setId('statusLabel')
  app.add(label);

  var handler = app.createServerHandler('loadData');
  var chk = app.createCheckBox('test').addValueChangeHandler(handler).setVisible(false).setValue(true,true).setId('chk');
  app.add(chk);

  return app;
}

function loadData(e) {
  var app = UiApp.getActiveApplication();

  Utilities.sleep(3000); //placeholder for some function that takes a long time.

  var label = app.getElementById('statusLabel');
  label.setText("Loaded the data from the spreadsheet");

  return app;
}


回答2:

Several other widgets have setValue(value, fireEvents) methods, eg. TextBox, PasswordTextBox etc. It seems like there is nothing unique about the CheckBox widget in implementing this technique.