阻止服务器处理程序触发

时间:2012-11-07 00:00:38

标签: google-apps-script

如果客户端处理程序(在同一小部件​​上)满足验证条件,我们是否可以停止触发服务器处理程序? UI

如果文本框为空,我不希望提交执行任何服务器功能。

function doGet() {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable()
     .setWidget(0, 0, app.createTextBox().setId('textbox'))
     .setWidget(0, 1, app.createButton('Submit').setId('submit'))
     .setWidget(0, 2, app.createLabel().setId('status'));

  var clientHandler = app.createClientHandler().validateNotMatches(app.getElementById('textbox'), ' ');

  var serverHandler = app.createServerHandler('submit').addCallbackElement(flex);

  app.getElementById('submit').addClickHandler(clientHandler).addClickHandler(serverHandler);

  app.add(flex);
  return app;

}

function submit(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('status').setText('Server handler fired');
  return app;
}

1 个答案:

答案 0 :(得分:2)

您不需要或不需要客户端处理程序,只需要服务器处理程序上的验证程序:

function doGet() {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable()
     .setWidget(0, 0, app.createTextBox().setId('textbox'))
     .setWidget(0, 1, app.createButton('Submit').setId('submit'))
     .setWidget(0, 2, app.createLabel().setId('status'));

  var serverHandler = app.createServerHandler('submit')
     .validateLength(app.getElementById('textbox'), 1, null)
     .addCallbackElement(flex);

  app.getElementById('submit').addClickHandler(serverHandler);

  app.add(flex);
  return app;

}

function submit(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('status').setText('Server handler fired');
  return app;
}

如果您想要一条消息说明哪里出了问题,可以添加:

var clientHandler = app.createClientHandler()
    .validateNotLength(app.getElementById('textbox'), 1, null)
    .forTargets(app.getElementById('status'))
    .setText('Cannot be empty');

app.getElementById('submit').addClickHandler(serverHandler)
    .addClickHandler(clientHandler);