Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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);
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top