商店中的成功和失败功能 - Ext JS [英] Success and Failure functions in a store - Ext JS
问题描述
我有一个请求,成功后,循环遍历JSON响应的每个属性,并将其添加到我的商店
:
I have a request which, on success, loops through each attribute of a JSON response and adds it to my 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')
}
});
我现在已经重新创建了请求
我的商店
。我需要做的是添加这些相同的成功和失败功能。
I have now recreated the request
above within my store
. What I need to do is add these same success and failure functions.
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)有没有办法在我的商店重新创建这些功能?
1) Is there any way to recreate these functions on my store?
2)以这种方式解码我的回复是将我的回复加载到我的商店的最佳方法?
2) Is decoding my response this way the best way to load my response into my store?
编辑
我在加载商店时使用回调函数:
I'm using the callback function when I load the store:
store.load({
params: { start: 0, limit: itemsPerPage },
callback: function(options, success, response, records) {
if (success) {
alert(response.responseText);
}
}
});
但是,我得到一个 undefined
在我的警报,它告诉我有0条记录加载。但是当我看到我在Firebug中的回复时,我看到我的JSON字符串返回的很好。
However, I'm getting an undefined
in my alert, and it's telling me there are 0 records loaded. But when I look at my response in Firebug I see my JSON string returned just fine.
推荐答案
您可以收听 exception-event 来捕获所有的存储错误。
并且在商店成功 load-event
You can listen for the exception-event on the proxy to capture all the store errors. And for success on the store load-event
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' }
],
listeners: {
load: function(store, records, successful, eOpts) {
if (successfull) {
alert('success');
}
}
},
proxy: {
type: 'ajaxwithpayload', //customized proxy to read "jsonData"
url: 'MCApp',
jsonData: searchquery,
reader: {
type: 'json',
root: 'elements'
},
listeners: {
exception: function(proxy, response, operation, eOpts) {
alert('exception');
}
}
}
});
或在加载调用本身:
store.load({
callback: function(records, operation, success) {
// ...
}
});
或如果您使用同步(保存已删除,修改,...)
or if you use sync (for saving removed, modified,...)
store.sync({
callback: function(batch, options) {
// ...
},
success: function(batch, options) {
// ...
},
failure: function(batch, options) {
// ...
}
});
这篇关于商店中的成功和失败功能 - Ext JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!