3

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

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

和这样的商店:

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'
    });

在我的应用程序中,当用户登录时,我想获得他的偏好。

我有这样的功能:

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");
        },

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

现在我得到的只是在萤火虫中显示的未定义。

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

{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}

我需要能够在该商店上调用加载方法,并在回调中从模型中获取特定属性。我的商店将永远有 1 件商品!(还有其他方法可以做到这一点吗?)

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

编辑 我已经更改了我的服务器脚本以返回这样的 json:

{"d":{"data":{"userID":"12-44-55","admin":true}}}

这很好用,不需要在对象周围添加额外的 []。

4

1 回答 1

7

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

{"d":[{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}]}

并将根设置为 d

reader : {
    type : 'json',
    root : 'd'
}

在回调之后,您将拥有一个包含一个元素的记录数组,您可以使用索引 0 轻松访问

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

编辑

其他不涉及门店的方法

执行一个简单的 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')    
            }
        });

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

于 2012-06-13T12:26:21.397 回答