最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I add additional parameters to an ExtJS handler? - Stack Overflow

programmeradmin3浏览0评论

I am using the ExtJS framework and I have the following handler that is used solely as a handler for a button:

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 am using the ExtJS framework and I have the following handler that is used solely as a handler for a button:

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?

Share Improve this question asked Mar 27, 2009 at 15:59 HuuuzeHuuuze 16.4k26 gold badges75 silver badges91 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

I would actually use Exts createDelegate prototype.

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
});

In 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.

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);
};
发布评论

评论列表(0)

  1. 暂无评论