我使用的ExtJS的框架和我有以下处理程序,仅作为处理程序的按钮:

var myButtonHandler = function(button, event){
   //code goes here
};

我的按钮定义如下所示:

var myButton = new Ext.Button({
       id : 'myButton',
       renderTo : 'mybutton',
       text : 'Save',
       handler : myButtonHandler,
       scope : this
    });

可以看到,处理程序接收的预期的“按钮”和“事件”。不过,我想通过一些额外的信息到我的处理程序。我该怎么做?

有帮助吗?

解决方案

我会实际使用的EXTS createDelegate方法的原型。

var appendBooleanOrInsertionIndex = 0; // Inserts the variables into the front of the function.
    appendBooleanOrInsertionIndex = true; // Appends the variables to the end of the arguments

var myButton = new Ext.Button({
   id : 'myButton',
   renderTo : 'mybutton',
   text : 'Save',
   handler : myButtonHandler.createDelegate(this, [param1, param2], appendBooleanOrInsertionIndex),
   scope : this
});

其他提示

在分机JS 4:

Ext.bind(myButtonHandler, this, [params array], true);

您可以使用一个很好的解决方案,布拉德利建议。下面是一个例子。 其中repeatsStore - 这是我想传递给一个按钮的处理程序的其他参数

Ext.create('Ext.panel.Panel', {
    name: 'panelBtn',
    layout: 'hbox',
    border: 0,
    items:[
        {xtype: 'button', text: 'Add', name:'addBtn',
         handler : Ext.bind(this.addBtnHandler, this, repeatsStore, true)
        }
    ]
});

和处理程序应该有三个参数 - 前两个是标准的,最后是你的

addBtnHandler:function(button, event, repeatsStore)
{
}

我不知道它是什么,你想传递,但使用的包装可以帮助:

var myButtonHandler = function (button, event, additionalData){
   //code goes here
};

var myButton = new Ext.Button({
  id : 'myButton',
  renderTo : 'mybutton',
  text : 'Save',
  handler : handlerWrapper,
  scope : this
});

var handlerWrapper = function (button, event){
  // Fetch additional data
  var additionalData = "whatever";
  myButtonHandler(button, event, additionalData);
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top