简体   繁体   English

如何向ExtJS处理程序添加其他参数?

[英]How do I add additional parameters to an ExtJS handler?

I am using the ExtJS framework and I have the following handler that is used solely as a handler for a button: 我正在使用ExtJS框架,我有以下处理程序,它只用作按钮的处理程序:

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

My button definition looks like this: 我的按钮定义如下所示:

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

As you can see, the handler receives the expected "button" and "event". 如您所见,处理程序接收预期的“按钮”和“事件”。 However, I'd like to pass some additional information into my handler. 但是,我想将一些额外的信息传递给我的处理程序。 How would I do that? 我该怎么办?

I would actually use Exts createDelegate prototype. 我实际上会使用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
});

在Ext JS 4中:

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

You can use a good solution as Bradley has suggested. 你可以像布拉德利建议的那样使用一个好的解决方案。 Here is an example. 这是一个例子。 Where repeatsStore - it is additional parameter that I want to pass to a button handler. 其中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)
        }
    ]
});

And your handler should have three parameters - first two are standard, and last is your. 你的处理程序应该有三个参数 - 前两个是标准的,最后一个是你的。

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

I don't know what is it that you want to pass, but using a wrapper could help: 我不知道你想传递什么,但使用包装器可以帮助:

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM