0

I have written a quite complex script which calls a server handler to refresh elements in a grid at the press of a button.

For code re-use and consistent behaviour, I am wanting to call that server handler directly during the initial painting of the grid.

When the server handler gets called by clicking on the button, all expected widgets are available and can be queried with e.parameter.widget etc.

When I call the function directly and pass it the panel variable, the value of e is just 'AbsolutePanel'.

Is there some way I can emulate the addCallbackElement way of passing the entire panel and all widgets it contains to the function?

Or a way of automatically firing a server handler on script start?

Please forgive any syntax errors below, I have pruned 500 lines of code down to the pertinent bits!

Thanks

Tony

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

  var mainPanel = app.createAbsolutePanel();

  var monthsAbbr = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];

  var Dates = Array();
  var period = 5;
  var dateHidden = Array();
  var dayOfMonth = new Date(((period * 28) + 15887) * 86400000);
  var dateString = '';
  var dayOfWeek = 0;
  for (var i=0; i<84; i++) {
     dateString = dayOfMonth.getDate() + ' ' + monthsAbbr[dayOfMonth.getMonth()] + ' ' + (dayOfMonth.getFullYear() - 2000);
    Dates [i] = dateString;
    dateHidden[i] = app.createHidden('dates'+i, dateString).setId('dates'+i);
    mainPanel.add(dateHidden[i]);
    dayOfMonth = new Date(dayOfMonth.getTime() + 86400000);
 }

 var buttonReset = app.createButton('Reset').setId('buttonReset');

 var handlerChange = app.createServerHandler('myHandlerChange');
 handlerChange.addCallbackElement(mainPanel);

 mainPanel.add(buttonReset.addChangeHandler(handlerChange));

 app.add(mainPanel);

 myHandlerChange(mainPanel);

 return app;
}


function myHandlerChange(e) {
var app = UiApp.getActiveApplication();
Logger.log('Here are the widgets passed into the function: ' + Utilities.jsonStringify(e));

return app;
 }
Tony
  • 43
  • 1
  • 5

2 Answers2

0

may want some thing like...

function doGet {
  ...
  myHandlerChange();  // DON'T PASS THE UI WIDGET ITSELF 
  return app;
}


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

  if (e) { // DO SOMETHING WHEN 'e' EXISTS
    Utilities.jsonStringify(e));
  } 
  else { //  DO SOMETHING AT THE START, WHEN 'e' DOESN'T EXIST
    app.getElementById('dates0').setText('something');
  }

  return app;
}
Bryan P
  • 5,031
  • 3
  • 30
  • 44
  • Thanks Bryan, unfortunately the function does need to refer to the value of a lot of the widgets using e.parameter.widget so I am not sure this solution will help in my case. Serge's suggestion of triggering the event with a hidden checkbox will do the trick nicely. – Tony Oct 31 '13 at 22:28
0

You could use a checkBox (setVisible(false)) to trigger an event when you want it and without any user action, see the documentation here : "Sets whether the CheckBox should be checked and optionally fires an event if the value changes as a result of this call."

There are a few examples on the forum of such implementations : here or here

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131