【发布时间】:2014-03-17 16:22:56
【问题描述】:
我有一个请求,如果成功,它将遍历 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 中重新创建了requestabove。我需要做的是添加这些相同的成功和失败函数。
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 字符串返回得很好。
【问题讨论】:
-
为什么不看看他们网站上的例子呢?他们已经涵盖了这一点。
-
好吧,我承认我确实有点懒惰了。这可以用
callback完成吗? -
如果您不确定函数返回什么,您可以执行
console.log(arguments)=> 它会记录所有参数的数组,参数的顺序是错误的。 :) (或者您可以查看课程文档;)) -
嘿@VDP,再来一个! stackoverflow.com/questions/22481633/…
标签: extjs