Ext.Ajax.request 中的 javascript 范围问题
我有这个类,它自动从服务器生成 JSON 存储。商店是动态创建的,并且可以在 Firebug 的 DOM 中查看。 我认为我有一些范围问题。当我想在 Ext.Ajax.request 的回调函数“AdminSettings”存储中 console.log(Ext.getStore('AdminSettings')) (其中 AdminSettings 是创建的存储之一)返回,但如果我将 console.log(Ext.getStore('AdminSettings')) 放在回调函数之外的任何地方,我会在 firebug 中收到未定义的消息,并且我的商店未实例化。 请参阅代码注释以查看其实际效果。
Ext.Ajax.request({
url : './account/getadminstores',
callback : function(options, success, response) {
var json = Ext.decode(response.responseText);
var adminStores = new Array();
// setup and intitialize on the fly stores
for ( var key1 in json) {
var storeFields = new Array();
for ( var key2 in json[key1]) {// if (i==1){break;}
for ( var key3 in json[key1][key2]) {
storeFields.push(key3);}
break;};
Ext.define('MA.store.' + key1, {
extend : 'Ext.data.Store',
fields : storeFields,
storeId : key1,
data : json[key1]
});
Ext.create('MA.store.'+key1);};
console.log(Ext.getStore('AdminSettings'));
//returns MA.store.AdminSettings in firebug and everything is fine
}
});//eof Ext.Ajax.request
console.log(Ext.getStore('AdminSettings'));
//returns undefined which is strange
I have this class which automatically generates JSON stores from server. Stores are created on the fly and are viewable in Firebug's DOM.
I think i have some scope issues.When i want to console.log(Ext.getStore('AdminSettings')) (which AdminSettings is one of the created stores) inside Ext.Ajax.request`s callback function, 'AdminSettings' store is returned but if i put console.log(Ext.getStore('AdminSettings')) everywhere outside callback function,i get undefined message in firebug and my store is not instantiated.
See code comments to see it in action.
Ext.Ajax.request({
url : './account/getadminstores',
callback : function(options, success, response) {
var json = Ext.decode(response.responseText);
var adminStores = new Array();
// setup and intitialize on the fly stores
for ( var key1 in json) {
var storeFields = new Array();
for ( var key2 in json[key1]) {// if (i==1){break;}
for ( var key3 in json[key1][key2]) {
storeFields.push(key3);}
break;};
Ext.define('MA.store.' + key1, {
extend : 'Ext.data.Store',
fields : storeFields,
storeId : key1,
data : json[key1]
});
Ext.create('MA.store.'+key1);};
console.log(Ext.getStore('AdminSettings'));
//returns MA.store.AdminSettings in firebug and everything is fine
}
});//eof Ext.Ajax.request
console.log(Ext.getStore('AdminSettings'));
//returns undefined which is strange
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ext.Ajax.request 是异步的。您的最终 console.log 调用不会返回任何内容,因为在执行该请求时,请求尚未完成。任何依赖于请求回调结果的代码也需要由回调执行。
Ext.Ajax.request is asynchronous. Your final console.log call returns nothing because at the time it is executed the request has not yet completed. Any code depending on the results of the request's callback will need to be executed by the callback as well.
这是由于 javascript 的异步特性,
callback
在 ajax 请求完成后才会被调用,因此 Ext 数据存储也不会被定义。脚本的执行顺序是:
This is due to the asynchronous nature of javascript,
callback
doesn't get called until after the ajax request finishes, so the Ext data store doesn't get defined either.The order of execution of your script is: