javascript - 处理类内的事件(原型(prototype))

标签 javascript event-handling google-apps-script prototype google-docs-api

我正在重写问题以使其更容易回答(我希望)。我想在类(原型(prototype))中有一个按钮,但我希​​望处理程序也位于类中。如果处理程序在类外,则工作正常,但如果在类内,则无法找到它。

预先感谢您的帮助。

function myWindow(message) {  
  this.SS = SpreadsheetApp.getActiveSpreadsheet(); 
  this.App = UiApp.createApplication();
  this.App.setTitle(message);
  this.Panel = this.App.createVerticalPanel();
  this.App.add(this.Panel);
  this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
};

myWindow.prototype = {
  constructor: myWindow,
  handler: function (e) 
  {
    Browser.msgBox('Click');
  },
  show: function()
  {
    this.Panel.add(this.Button);
    this.SS.show(this.App);
  }
};

function Run() {
  var theWindow = new myWindow('Hello World');
  theWindow.show();
}

单击“确定”按钮时出现的错误是“找不到脚本函数处理程序”

最佳答案

好吧,我已经做了很多阅读,虽然没有明确说明,但 createServerHandler 似乎只是为全局函数设计的,UIAPP 中没有提供任何其他内容。这意味着我所要求的无法完成。

解决方案必须看起来像

function myWindow(message) {  
  this.SS = SpreadsheetApp.getActiveSpreadsheet(); 
  this.App = UiApp.createApplication();
  this.App.setTitle(message);
  this.Panel = this.App.createVerticalPanel();
  this.App.add(this.Panel);
  this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
}

myWindow.prototype = {
  constructor: myWindow,
  show: function() {
    this.Panel.add(this.Button);
    this.SS.show(this.App);
  }
};

function handler(e) {
  Browser.msgBox('Click');    
  };

function Run() {
  var theWindow = new myWindow('Hello World');
  theWindow.show();
};

这不是 flash 但可行。

关于javascript - 处理类内的事件(原型(prototype)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207014/

相关文章:

javascript - 单击链接时显示相关 div

javascript - 如何在嵌套 GridView 中将鼠标悬停在图像上时获取 jquery 弹出窗口

c# - 分离空事件处理程序

javascript - 在浏览器中关闭 WebSocket 连接时是否必须手动删除事件监听器?

google-apps-script - Google Apps 脚本重定向 uri 错误

pdf - 将多个 PDF 合并为一个 PDF

google-apps-script - 在 Google Apps 脚本中使用 GMail API 发送电子邮件

javascript - 将文本与大小包含的背景图像底部对齐

javascript - symfony 多个 Controller 操作在独特的模板中呈现 - 达到最大嵌套级别

c++ - SetWinEventHook 不捕获任何事件