PDA

View Full Version : [FIXED-84][3.0.0]Store unable to restore destroyed records upon write fail.



MaximGB
8 Aug 2009, 6:04 PM
The Ext.data.Store::onDestroyRecords() has a bug which prevents it to restore destroyed records if a server returns {success: false} result.

Original code

onDestroyRecords : function(success, rs, data) {
// splice each rec out of this.removed
rs = (rs instanceof Ext.data.Record) ? [rs] : rs;
for (var i=0,len=rs.length;i<len;i++) {
this.removed.splice(this.removed.indexOf(rs[i]), 1);
}
if (success === false) {
// put records back into store if remote destroy fails.
// @TODO: Might want to let developer decide.
for (i=rs.length-1;i>=0;i--) {
this.insert(rs[i].lastIndex, rs[i]); // <-- lastIndex set in Store#destroyRecord
}
}
}
This happens due to rs and this.removed seem to be the same array, so the second for loop loops on the empty array. The fix is pretty straightforward.

Fix:

onDestroyRecords : function(success, rs, data) {
// splice each rec out of this.removed
rs = (rs instanceof Ext.data.Record) ? [rs] : [].concat(rs);
for (var i=0,len=rs.length;i<len;i++) {
this.removed.splice(this.removed.indexOf(rs[i]), 1);
}
if (success === false) {
// put records back into store if remote destroy fails.
// @TODO: Might want to let developer decide.
for (i=rs.length-1;i>=0;i--) {
this.insert(rs[i].lastIndex, rs[i]); // <-- lastIndex set in Store#destroyRecord
}
}
}

evant
8 Aug 2009, 11:54 PM
Can you post a test case for this? Using the SVN version:



Ext.onReady(function(){
var s = new Ext.data.JsonStore({
url: 'data.asp',
fields: ['a'],
root: 'data',
id: 'id',
successProperty: 'success',
autoSave: true,
autoLoad: true,
writer: new Ext.data.JsonWriter()
});

(function(){
s.baseParams.foo = 'x';
s.removeAt(0);
(function(){
console.log(s.getCount());
}).defer(500);
}).defer(500);
});


If the server returns false, I see 4 as the count. If the server returns true, I see 3.

MaximGB
9 Aug 2009, 10:28 AM
I forgot to mention that I use store with batch: true and autoSave: false config options.

Here is the test case demo page URL
http://www.max-bazhenov.com/dev/tests/store_batch_destroy.php

p.s. It would be good if Ext.debug namespace were included into ext-all-debug.js

evant
9 Aug 2009, 11:13 AM
Fixed in SVN r5010, thanks.