Ext.Ajax.request 中的 javascript 范围问题

发布于 2024-12-05 09:36:15 字数 1152 浏览 1 评论 0原文

我有这个类,它自动从服务器生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2024-12-12 09:36:15

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.

谁对谁错谁最难过 2024-12-12 09:36:15

这是由于 javascript 的异步特性,callback 在 ajax 请求完成后才会被调用,因此 Ext 数据存储也不会被定义。

脚本的执行顺序是:

  1. Ajax 请求被初始化
  2. console.log 被执行(未定义,因为数据存储尚未定义)
  3. javascript 正在等待来自 ./account/getadminstores 的 ajax 响应
  4. 响应进来,回调函数是称为(定义数据存储)

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:

  1. Ajax request is initialized
  2. console.log is executed (undefined since the datastore hasn't been defined yet)
  3. javascript is waiting for an ajax response from ./account/getadminstores
  4. response comes in, callback function is called (which defines the data store)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文