PDA

View Full Version : Accessing scope



simon
7 Aug 2009, 1:01 AM
If I create some ext element in onReady function, how can I acces to their selected value outside the onReady scope?



Ext.onReady(function() {
var cmbPriority = new Ext.ux.TwinCombo({
onTrigger2Click: function() {
this.clearValue();
},
trigger2Class: 'x-form-clear-trigger',
xtype: 'combo',
id: 'priority',
name: 'priority',
mode: 'local',
emptyText: 'select priority',
store: storePriority,
typeAhead: false,
triggerAction: 'all',
selectOnFocus: true,
displayField: 'name',
listClass: 'levi',
valueField: 'id',
width: 140
});
});

//var val=cmbPriority.getValue(); this doesn't work
//var val=Ext.getCmp('priority').getValue(); this also doesn''t work

steffenk
7 Aug 2009, 3:45 AM
this is a question of application design.
It's obvious that the part you wrote after Ext.onReady will be executed before, so it can't work.

Have a look at the learning center how app design is done, i prefer to prepare the classes before (using class structs through) and then initialize the app completely in Ext.onReady.

Condor
7 Aug 2009, 4:06 AM
The last one should work:

var val=Ext.getCmp('priority').getValue();
as long as you call it after the field has been created (e.g. in an event handler).

Using it outside Ext.onReady() like you do now will execute your code BEFORE the Ext.onReady callback function is called.

VinylFox
7 Aug 2009, 4:13 AM
Just to clarify what steffenk is saying...


var val=cmbPriority.getValue();
var val=Ext.getCmp('priority').getValue();

The first line has a scope & timing problem, the cmbPriority var is scoped within your onReady, so you will not be able to access it outside of the onReady unless it is var'd outside of the onready.

Now about the timing problem, anything inside the onReady will not be available until the document is ready, which can be a few milliseconds after your JavaScript runs. So both your var val lines are executed right away, while everything in the onReady runs a few milliseconds later.

Both the lines you have will work, but they either need to be inside the onReady, or attached to some event that is tied to a user interaction that would come after the page has loaded.