views:

4653

answers:

2

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?

+1  A: 

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);
};
Seb
I'm guessing "var handlerWrapper()" should be "function handlerWrapper()". Yes?
Huuuze
Yeah, sorry... corrected that now. Thanks.
Seb
+2  A: 

I would actually use Exts createDelegate prototype.

var myButton = new Ext.Button({
       id : 'myButton',
       renderTo : 'mybutton',
       text : 'Save',
       handler : myButtonHandler.createDelegate(this, [params], true),
       scope : this
    });
Ballsacian1
This is a more elegant answer than Seb's. The work of creating a wrapper is hidden by createDelegate.Optionally, the user could add a custom property to the button and inspect that from the handler. I still prefer createDelegate.
Juan Mendes