PDA

View Full Version : Why "this" does not hold a value within the loader



Pachat
21 Sep 2010, 5:12 AM
Here is an extension to TreePanel

right_tree = Ext.extend(Ext.tree.TreePanel, {
loader: new Ext.tree.TreeLoader({
dataUrl:'get-nodes.php',
baseParams: {path:'extjs',tab:this.tab_index} // custom http params
}),
listeners: {
click: function(obj, e){
alert('click tab_index='+this.tab_index);
}
}
});

tree0 = new right_tree({tab_index: 2});
When I click on the tree0, I correctly get
click tab_index=2But within the loader, this.tab_index is null.

How can I pass the value of this.tab_index to the loader ?

tryanDLS
21 Sep 2010, 11:11 AM
Please post in the correct forum - extensions/plugins is for sharing working code, not asking for help. Please don't double post - thread moderation is on.

jay@moduscreate.com
23 Sep 2010, 4:34 AM
Your problem is that you're extending incorrectly! Each instance of "right_tree" will share the exact same tree loader!

Same goes for the listeners. Set those two properties inside of a constructor or initComponent.

jay@moduscreate.com
23 Sep 2010, 4:35 AM
You also might be able to find the problem by using firebug or a similar debugger instead of using alerts to debug.

You'd see that "this" refers to the scope of the object for which your code is executing at the time your extension is being created.

You should consider learning Object Oriented JavaScript.

Pachat
23 Sep 2010, 5:11 AM
You also might be able to find the problem by using firebug or a similar debugger instead of using alerts to debug.

You'd see that "this" refers to the scope of the object for which your code is executing at the time your extension is being created.

You should consider learning Object Oriented JavaScript.

Got it.

You could not say better.
Before I received your answer, I watched your excellent video on Ext JS Screencast: Abstract Classes (http://vimeo.com/12017473)
(and send you some $$$ to help you acquire some material).
Also, I'm reading chapter 15 of your book (MEAP)

Althought (or because) of a long experience in OO with other languages, it's not easy for me to cacth a class less language.
But with your help I hope to succeed.

Continue the good work.

jay@moduscreate.com
23 Sep 2010, 5:14 AM
Well thank you for the kind words and support :).

JavaScript is relatively simple when you do not compare its inheritance model with other languages that you know :)