PDA

View Full Version : Scope, handlers, toolbars, oh my



pazx
28 Jun 2007, 6:43 PM
This is killing me... what am I doing wrong to reference root in someHandler()?





var FileManager = function(){

...

return {
init: function() {
...
...

var root = new Tree.AsyncTreeNode(...);

var tb = new Ext.Toolbar('atoolbar');
tb.add({
text:'menu name',
menu: {
items: [{
text: 'button name',
handler: this.someHandler
scope: this
}]
},
});

},

someHandler : function() {
// Why can't I get to the root var?
}

}

tryanDLS
28 Jun 2007, 6:50 PM
root is local to init, not the FileManager object. I suggest your refer to the sticky 'scope' thread at the top of this forum, for resources discussing in more detail. There's also a section in the Learning section on the home page.

fay
29 Jun 2007, 1:30 AM
Saki's http://extjs.com/forum/showthread.php?t=6564 is very good. For your specific problem:


var FileManager = function(){

var root;

return {
init: function(){
...
root = new Tree.AsyncTreeNode(...);
...
},

someHandler: function(){
root....
}
...

pazx
30 Jun 2007, 3:17 PM
thanks for the help!

for some reason that escapes me now i had thought it was bad practice to define variables outside of the init() scope, but i had to resort to that for a few other things. i'll do that for this too. the other easy option was to move someHandler() itself inside init(). i guess my question was more one of best practices than anything else.

thanks again.