PDA

View Full Version : Reference class inside callback function



simon
7 May 2014, 11:45 PM
Inside controller:


onPanelRender: function (abstractcomponent, options) {
var me = this;
me.getMenuStore().load(function(records, op, success){
//here me is null, why?
var menuPanel = Ext.ComponentQuery.query('mainmenu')[0];//this works
}


I would like to access the menuPanel inside stole load callback function.
One way is to use Ext.ComponentQuery and it works.
I would like to do also with me variable(which has reference to controller).
How can I do that?

Thanks

skirtle
8 May 2014, 5:00 PM
Unless you're nulling out the me variable further down it should be accessible in the callback. You haven't said why you believe me is null but I think you're misinterpreting what you're seeing.

simon
8 May 2014, 11:30 PM
ou are right, it must be, since i never set it to nothing.
Obvious, the problem is that I look for "me" with chrome debugger?
If i set breakpoint inside call back function, at line: " var menuPanel = ...." and I look for me variable in console window, I get the following error:
"ReferenceError: me is not defined"

Also if i put me as watch expression it is unknown.

But, if i write some alert:



me.getMenuStore().load(function(records, op, success){
var menuPanel = Ext.ComponentQuery.query('mainmenu')[0];
alert(me.id);


I get "Menu" aas alert. So me exists, the problem is in chrome debugger. Why it doesn't show "me" variable?

Thank you,
Simon

jsakalos
8 May 2014, 11:37 PM
Debugger shows only local variables by default and me is defined one level up. Add it to "Watch expressions" and you should see the value. Note: Do NOT assign any value to me inside the load callback.

simon
8 May 2014, 11:38 PM
me.getMenuStore().load(function(records, op, success){
var menuPanel = Ext.ComponentQuery.query('mainmenu')[0];
console.log(me.id);
debugger;//now me exists


If I write without referencing me before debugger, me doesn't exists:



me.getMenuStore().load(function(records, op, success){
var menuPanel = Ext.ComponentQuery.query('mainmenu')[0];
debugger;//now me is not defined!


Is there some setting in chrome debugger? Otherwise it is very hard to debug if you don't see all variables?

skirtle
8 May 2014, 11:40 PM
It will appear in the 'Closure' section of 'Scope Variables' but only if you use it somewhere in the function. Un-used variables are automatically discarded to save resources.

simon
8 May 2014, 11:42 PM
Sakalos,

also in watch expression me is unknown. It is shown only if I reference it. What now. I can't find any setting in chrome to show all my variables.

I'm not setting any value to "me" inside the load callback. But If i would is there some problem?
Can you explain a little bit?

Thank you

simon
8 May 2014, 11:50 PM
Thank you Skirtle.
Sometimes I watch for available variables in debugger to understand what is happening.
And to see its properties or methods, and so on...It would be nice to have some option: "show all"

But now at least i know what is happening. Before I was very confused since "me" was null and it was not in scope with my understanding. I thought ext 4.xx changed some javascript rules :)

skirtle
9 May 2014, 12:18 AM
A discussion of the same problem on SO:

http://stackoverflow.com/questions/21917271/my-function-somehow-doesnt-have-access-to-its-parent-closure-is-missing-varia

simon
9 May 2014, 1:19 AM
Do you maybe know what Sakalos meant by this:
" Do NOT assign any value to me inside the load callback."