Ext JS 4存储加载回调

Mis*_*siu 3 json extjs4 extjs4.1

我正在尝试为我的学校项目构建一个简单的用户设置功能.
我创建了一个这样的数据模型:

Ext.define('Urlopy.Model.UserPreferences', {
        extend : 'Ext.data.Model',
        idProperty : 'userID',
        fields : [{
                    name : 'userID',
                    type : 'string'
                }, {
                    name : 'admin',
                    type : 'bool'
                }]
    });
Run Code Online (Sandbox Code Playgroud)

和这样的商店:

Ext.define('Urlopy.Store.UserPreferences', {
        extend : "Ext.data.Store",
        autoLoad : false,
        autoSync : true,
        batch : false,
        proxy : {
            type : 'ajax',
            sortParam : undefined,
            startParam : undefined,
            pageParam : undefined,
            limitParam : undefined,
            noCache : false,
            headers : {
                "Content-Type" : "application/json; charset=utf-8"
            },
            api : {
                read : 'services/UserPreferences.asmx/Get',
                update : 'services/UserPreferences.asmx/Update'
            },
            reader : {
                type : 'json',
                root : 'd.data'
            },
            writer : {
                type : 'json',
                encode : false,
                writeAllFields : true,
                root : 'data'
            }
        },
        model : 'Urlopy.Model.UserPreferences'
    });
Run Code Online (Sandbox Code Playgroud)

在我的应用程序用户登录时,我想得到他的偏好.

我有这样的功能:

onLogin : function() {

            this.createGlobalStores();

            this.userPreferenceStore.load({

                        callback : function(r, options, success) {
                            console.log(r.data)
                        }

                    });

        },
        createGlobalStores : function() {
            this.userPreferenceStore = Ext.create("Urlopy.Store.UserPreferences");
        },
Run Code Online (Sandbox Code Playgroud)

我想在回调中做的是获取用户ID并显示它,即使使用简单的警报.

现在我所得到的都是未定义的,在firebug中显示.

来自服务器的json数据如下所示:

{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
Run Code Online (Sandbox Code Playgroud)

我需要能够在该商店调用load方法,并在回调中从模型中获取特定属性.我的商店总有1件商品!(还有另一种方法吗?)

欢迎任何关于为什么不起作用的想法!

编辑 我已经改变了我的服务器脚本,像这样返回json:

{"d":{"data":{"userID":"12-44-55","admin":true}}}
Run Code Online (Sandbox Code Playgroud)

这工作正常,没有必要在对象周围添加额外的[].

nsc*_*rob 7

首先,商店的负载需要一个数组,其次你设置代理的根是错误的.因为你的json中没有d.data对象.简单的解决方法是将json数据编码为具有一个元素的数组,并将该数组的标记设置为"data"...

{"d":[{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}]}
Run Code Online (Sandbox Code Playgroud)

并将根设置为d

reader : {
    type : 'json',
    root : 'd'
}
Run Code Online (Sandbox Code Playgroud)

在回调之后,您将拥有一个记录数组,其中包含一个元素,您只需使用索引0即可访问该元素

callback : function(records, options, success) {
    console.log(records[0].data.userID +" " + records[0].data.admin);
}
Run Code Online (Sandbox Code Playgroud)

编辑

不涉及商店的其他方法

做一个简单的ajax请求,如:

Ext.Ajax.request({
            url: 'services/UserPreferences.asmx/Get',
            params: {/*if you want any extra params to be sent*/},
            scope: this,
            success: function (result) {
                var response = Ext.decode(result.responseText);
                //this is for your initial json
                alert(response.d.userID);
            },
            failure: function (result) {
                alert('something failed')    
            }
        });
Run Code Online (Sandbox Code Playgroud)

我不知道你对数据做了什么,但是例如你可以将它加载到表单中,对于更新方法,你将拥有表单的提交选项.可以使用url配置发送数据的位置.