2009-03-27 9 views
5

Sto usando il quadro ExtJS e ho il seguente gestore che viene utilizzato esclusivamente come un gestore per un pulsante:Come aggiungere parametri aggiuntivi a un gestore ExtJS?

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

La mia definizione pulsante appare così:

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

Come si può vedere , il conduttore riceve il "pulsante" e l'"evento" previsti. Tuttavia, mi piacerebbe passare alcune informazioni aggiuntive nel mio gestore. Come potrei farlo?

risposta

7

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

Questa è una risposta più elegante di quella di Seb. Il lavoro di creazione di un wrapper è nascosto da createDelegate. Facoltativamente, l'utente può aggiungere una proprietà personalizzata al pulsante e ispezionarla dal gestore. Continuo a preferire createDelegate. –

+0

Quindi, come gestiresti questo all'interno della funzione ** myButtonHandler ** per usare i tuoi [parametri]? Solo myButtonHandler ([params])? – dmackerman

+0

Sono andato avanti e ho modificato l'esempio per mostrare meglio cosa si può fare con createDelegate. Anche in Ext4 e SenchaTouch createDelegate è stato spostato fuori dal prototipo della funzione ed è ora accessibile solo da Ext.createDelegate (functionToDelegate, scope, args, appendOrInsertIndex) – Ballsacian1

3

Non so che cosa è che si desidera passare, ma utilizzando un wrapper potuto fare 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); 
}; 
+0

Suppongo che "var handlerWrapper()" debba essere "function handlerWrapper()". Sì? – Huuuze

+0

Sì, mi dispiace ... corretto ora. Grazie. – Seb

4

In Ext JS 4:

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

+1 Questa è una soluzione 1 linea veramente bella per ExtJS 4+. – BenSwayne

+0

Questo vale anche per ExtJS 3.4 (L'uso di Ext.createDelegate) – lcguida

5

È possibile utilizzare una buona soluzione come Bradley ha suggerito. Ecco un esempio. Dove repeatsStore - è un parametro aggiuntivo che voglio passare ad un gestore di pulsanti.

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 gestore deve avere tre parametri: i primi due sono standard e l'ultimo è il tuo.

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