Domanda

Sto usando il quadro ExtJS e ho il seguente gestore che viene utilizzato esclusivamente come un gestore per un pulsante:

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

La mia definizione pulsante simile al seguente:

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

Come si può vedere, il gestore riceve il "tasto" atteso e "evento". Tuttavia, mi piacerebbe passare alcune informazioni aggiuntive nel mio gestore. Come potrei farlo?

È stato utile?

Soluzione

mi sarebbe effettivamente utilizzare Exts createDelegate prototipo.

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

Altri suggerimenti

In Ext JS 4:

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

È possibile utilizzare una buona soluzione come Bradley ha suggerito. Ecco un esempio. Dove repeatsStore -. È ulteriore parametro che voglio passare a un gestore di pulsante

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

E il vostro gestore deve avere tre parametri -. Primi due sono di serie, e l'ultimo è il vostro

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

Non so che cosa è che si desidera passare, ma utilizzando un wrapper potrebbe contribuire a:

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);
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top