PDA

View Full Version : events and scope



kmchen13
28 Oct 2011, 11:58 AM
Hi everybody. I get that headake since 2 days and have to ask for some help. I have a script like this:

MyComponent = Ext.extend(Ext.Panel, { ...


this.on({
'afterrender': function(cid){

this.getEl().on('contextmenu', function(e) {
e.preventDefault();

if (!this.ctxMenu) {
this.ctxMenu = new Ext.menu.Menu({
items : [{
text : "menu1",
handler: this.action1()
}
...
}
}


action1 : function{
...
}
}
The problem is that "this" in ctnMenu is not the right scope to access to action1.
I do not know how to change the scope. :((

Uberdude
1 Nov 2011, 2:36 AM
Read the documentation for "on" which is shorthand for addListener. Your brackets are unbalanced, so I have fixed them to make it clear where scope arguments go.




MyComponent = Ext.extend(Ext.Panel, { ...
this.on({
'afterrender': function(cid) {

this.getEl().on('contextmenu', function(e) {
e.preventDefault();

if (!this.ctxMenu) {
this.ctxMenu = new Ext.menu.Menu({
items : [{
text : "menu1",
handler: this.action1()
}]
//... more menu config
}) // close menu constructor
} // close if
}, this); // scope is 3rd arg to 'on' in single event style
}, // close afterrender function
scope: this // named scope in object-config style 'on'
}); // close on

action1 : function{
...
}
}

kmchen13
4 Nov 2011, 2:44 AM
Yes it works that way. Thanks.