我正在尝试为我的学校项目构建一个简单的用户设置功能。
我创建了一个像这样的数据模型:
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}}}
这很好用,不需要在对象周围添加额外的 []。