PDA

View Full Version : Controller function with params



env0ke
9 Dec 2011, 4:11 AM
Hi all,

I try create function in controller of my app:


init:function () { this.control({
'#all-incidents-grid button[action=edit]':{
click:this.editIncident()
}
});
},
...
editIncident:function () {
Ext.widget('EditWindow');
this.getEditForm().loadRecord(this.getAllGrid().getSelectionModel().getSelection()[0]);
this.getEditWindow().show();
}


It working, but i need a function with param for reusable:


init:function () { this.control({
'#all-incidents-grid button[action=edit]':{
click:this.editIncident(this.getAllGrid())
}
});
},
...
editIncident:function (grid) {
Ext.widget('EditWindow');
this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
this.getEditWindow().show();
}


It not working :( .

arthurakay
9 Dec 2011, 10:48 AM
init:function () {
this.control({
'#all-incidents-grid button[action=edit]':{
click:this.editIncident(this.getAllGrid())
}
});
},
...
editIncident:function (grid) {
Ext.widget('EditWindow');
this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
this.getEditWindow().show();
}



First, you don't want to do this:

click:this.editIncident(this.getAllGrid())

That sets the "click" event to the result of your function. You want to set the click event to the function itself:


click:this.editIncident //NO PARENTHESES!

Second, why do you need to pass the argument? Just access that variable inside the handler:



editIncident:function () {
var grid = this.getAllGrid();
Ext.widget('EditWindow');
this.getEditForm().loadRecord(grid.getSelectionModel().getSelection()[0]);
this.getEditWindow().show();
}


The only other thing you might have to do is change the "scope" of the event handler so that "this" is the correct object.

env0ke
10 Dec 2011, 8:39 AM
arthurakay (http://www.sencha.com/forum/member.php?48511-arthurakay) thx for your answer.

If I have a general class of the grid and I want to create 5 objects of this class and ask each of them similar function handler clicks. I have to write 5 similar functions, which will differ only in reference to the grid i want?

Sorry of my bad english, google translate.

tobiu
10 Dec 2011, 10:33 AM
you can put a "forwarding" method between them.
like:



click: function(default params here) {
this.myFunction(myCustomParam,...);
},
scope: this