Json ExtJS4存储加载回调
我正在尝试为我的学校项目构建一个简单的用户设置功能。Json ExtJS4存储加载回调,json,extjs4,extjs4.1,Json,Extjs4,Extjs4.1,我正在尝试为我的学校项目构建一个简单的用户设置功能。 我创建了如下数据模型: Ext.define('Urlopy.Model.UserPreferences', { extend : 'Ext.data.Model', idProperty : 'userID', fields : [{ name : 'userID', type : 'string'
我创建了如下数据模型:
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'
});
{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
{"d":{"data":{"userID":"12-44-55","admin":true}}}
在我的应用程序中,当用户登录时,我希望获得他的首选项
我有这样一个函数:
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");
},
我想在回调中做的是获取userID并显示它,甚至使用简单的警报
现在我得到的只是firebug中显示的未定义
来自服务器的json数据如下所示:
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'
});
{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
{"d":{"data":{"userID":"12-44-55","admin":true}}}
我需要能够在该存储上调用load方法,并在回调中从模型中获取特定属性。我的商店将始终有一个项目!(还有别的方法吗?)
欢迎任何关于为什么这不起作用的想法
编辑
我已更改服务器脚本以返回json,如下所示:
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'
});
{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
{"d":{"data":{"userID":"12-44-55","admin":true}}}
这很好,不需要在对象周围添加额外的[]。首先,存储的加载需要一个数组,其次,代理的根设置错误。因为json中没有d.data对象。简单的修复方法是将json数据编码为一个包含一个元素的数组,该数组的标记为“data” 把根设为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。Hmm..我会马上修复它吗?是否有其他选项可以加载一个对象而不是数组?我知道我可以使用Store,但也许还有另一种方法?谢谢你的编辑:)我想要的是一个保存所有用户配置的全局对象,我将能够读取设置并更新它们。我已经做到了。我用基于你的解决方案编辑了我的问题。