我有一个请求,在成功时,循环遍历JSON响应的每个属性,并将其添加到我的store
:
var request = Ext.Ajax.request({
url: 'MCApp',
jsonData: searchquery,
params: {
start: 0,
limit: itemsPerPage
},
success: function(response) {
mainresponse = response.responseText;
if (mainresponse.length == 0) {
alert('No results returned');
return;
}
var decoded = Ext.decode(mainresponse);
for (var i = 0; i < decoded.elements.length; i++) { // loop over decoded data
var element = decoded.elements[i].element;
var model = {};
for (var x = 0; x < element.attributes.length; x++) { // loop over attributes
var attribute = element.attributes[x];
model[attribute.attrname] = attribute.attrvalue; // mapping element names & attributes
}
newstore.add(model); // implicitly cast data as Model
models[i] = model;
}
newstore.loadRawData(models);
},
failure: function() {
alert('Search Failed: Could not reach the server')
}
});
字符串
我现在已经在我的store
中重新创建了上面的request
。我需要做的是添加这些相同的成功和失败函数。
var store = Ext.create('Ext.data.Store', {
storeId: 'resultsetstore',
autoLoad: false,
pageSize: itemsPerPage,
fields: [
{ name: 'id', type: 'auto' },
{ name: 'name', type: 'auto' },
{ name: 'description', type: 'auto' }
],
proxy: {
type: 'ajaxwithpayload', //customized proxy to read "jsonData"
url: 'MCApp',
jsonData: searchquery,
reader: {
type: 'json',
root: 'elements'
}
success: { /* success functions */ },
failure: { /* failure functions */ }
}
});
型
以下是我的回应:
{
"elements":[
{
"element":{
"name":"Element Name",
"id":"Element ID",
"attributes":[
{
"attrname":"id",
"attrvalue":"This is the ID"
},
{
"attrname":"name",
"attrvalue":"This is the name"
},
//etc.
型
1)有没有办法在我的商店中重新创建这些功能?
2)这样解码我的响应是将我的响应加载到存储中的最佳方法吗?
编辑
我在加载商店时使用回调函数:
store.load({
params: { start: 0, limit: itemsPerPage },
callback: function(options, success, response, records) {
if (success) {
alert(response.responseText);
}
}
});
型
然而,我在警报中得到一个undefined
,它告诉我有0条记录加载。但是当我在Firebug中查看我的响应时,我看到我的JSON字符串返回得很好。
1条答案
按热度按时间ax6ht2ek1#
存储中的错误处理
您可以在代理上侦听异常事件以捕获所有存储错误,并在存储加载事件上侦听成功
字符串
或者在load调用本身中:
型
或者如果您使用同步(用于保存删除、修改等)
型