在ExtJS 4中调用服务器后取消store.remove
我使用的是ExtJS 4,有一个带有ajax代理和api的Ext.data.Store:在ExtJS 4中调用服务器后取消store.remove,extjs,Extjs,我使用的是ExtJS 4,有一个带有ajax代理和api的Ext.data.Store: var gridStore = Ext.create('Ext.data.Store', { autoSync: true, proxy: { type: 'ajax', api: { read: 'myurl', create: 'myurl', update: 'myurl',
var gridStore = Ext.create('Ext.data.Store', {
autoSync: true,
proxy: {
type: 'ajax',
api: {
read: 'myurl',
create: 'myurl',
update: 'myurl',
destroy: 'myurl'
},
reader: {
type: 'json',
successProperty: 'success',
root: 'data',
messageProperty: 'message'
},
writer: {
type: 'json',
writeAllFields: false,
root: 'data'
},
listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show({
title: 'Server error',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
}
...
当我使用update函数时,我的服务器返回一个json对象,并带有success:false
(因为他输入了错误的内容),我关联的网格中的字段仍然标记为已更改,用户可以选择更改其错误的值
那很好
但是当我从存储中删除一个记录时
var store = Ext.StoreManager.lookup('gridStore');
store.remove(store.getById(id));
…然后ExtJS首先从存储中删除此记录,然后调用ajax api。因此,当销毁api返回success:false
时,消息将显示为异常,就像在更新api中一样,这很好,但我的记录已从存储中删除!例如,来自服务器的异常说明您无法删除此记录,因为它已经在存储中删除
如何在服务器同步后取消删除存储?如果服务器返回success:false
,我希望记录保留在存储中
有什么想法吗?也许是虫子
更新解决方案 基于Ryan的anwer,我对异常侦听器进行了如下修改,效果非常好:
listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show(...);
// get the removed records and insert them where they have been
var removedRecords = gridStore.getRemovedRecords();
for(var i=0; i<removedRecords.length; i++){
var record = removedRecords[i];
gridStore.insert(record.index, record);
}
}
}
侦听器:{
异常:函数(代理、响应、操作){
Ext.MessageBox.show(…);
//获取已删除的记录并将其插入已删除的位置
var removedRecords=gridStore.getRemovedRecords();
对于(var i=0;i仅扩展您给出的代码,特别是侦听器
区域:
listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show({
title: 'Server error',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
gridStore.add(gridStore.getRemovedRecords());
}
}
我使用的是model.destroy,这是我用于从网格中删除单个条目的方法:
text : 'Delete',
itemId : 'delete',
scope : this,
handler : function() {
var selection = grid.getView().getSelectionModel().getSelection()[0];
if(selection) {
selection.destroy({
failure : function() {
console.log('Record could not be deleted.');
},
success : function() {
store.remove(selection);
console.log('Record successfuly removed.');
},
});
}
}
插入技术对我不起作用,删除的记录在下一次同步操作时保持标记为删除。为此,我使用Ext.data.Store.rejectChanges()
。我在同步时使用回调函数“success”、“failure”或“callback”。
我希望这个方法能帮助你
store.remove(records);
store.sync({
success: function (proxy, operations) {
// pop success message
}, failure: function (proxy, operations) {
// resume records
store.rejectChanges();
}
});
可能是一个bug,或者需要深入源代码以查看发生了什么。嗯。这是一个有趣的问题。我以前看到过,但我通过重新加载存储来克服这个问题。效率低下,但对于小数据来说也不错。不错:)但是您忘记添加gridStore.removed=[]
否则如果您再次删除该项,您将有重复项(那么它仍然在移除的阵列中)非常感谢,这是正确的方法。我稍微更改了您的解决方案,看看我更新的问题,因为如果您只添加记录,它们将添加到列表的末尾,但它们应该保持在以前的位置。这不是默认方法,是吗?您使用了吗?该方法存在于ExtJS 4.1.1a中。不确定4.0或版本这应该是正确的答案。很好。只适用于单张唱片。