gpt4 book ai didi

json - Ext JS 4 存储加载回调

转载 作者:行者123 更新时间:2023-12-01 00:35:38 26 4
gpt4 key购买 nike

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

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 并显示它,甚至使用简单的警报。

现在我得到的只是在 Firebug 中显示的未定义。

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

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

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

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

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

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

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

最佳答案

首先,商店的加载需要一个数组,其次,您将代理的根设置错误。因为你的 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。

关于json - Ext JS 4 存储加载回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com