Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 928
Huuuze Avatar asked Mar 27 '09 15:03

Huuuze


4 Answers

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
});
like image 106
Ballsacian1 Avatar answered Nov 08 '22 08:11

Ballsacian1


In Ext JS 4:

Ext.bind(myButtonHandler, this, [params array], true);
like image 35
Bradley Avatar answered Nov 08 '22 10:11

Bradley


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)
{
}
like image 41
Oleksandr_DJ Avatar answered Nov 08 '22 08:11

Oleksandr_DJ


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);
};
like image 27
Seb Avatar answered Nov 08 '22 09:11

Seb