PDA

View Full Version : how to recover a store?



atian25
10 Sep 2011, 9:21 PM
Model:

Ext.define('Voting', {
extend: 'Ext.data.Model',
hasMany: [
{model: 'VotingQuestion', name: 'questions'}
]
}

and:

grid.bindStore(voting.questions())

then use RowEditing to add/edit/delete/resort the grid

after this, If I click a cancel button, is there any easy way to recover the 'questions' to init status (except reload store from server) ?

thanks

skirtle
12 Sep 2011, 7:57 AM
So long as the records haven't been committed you should be able to use reject():

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Model-method-reject

The docs imply there is an equivalent method on the store but in a quick search I wasn't able to track it down.

atian25
12 Sep 2011, 5:25 PM
I know reject.
but I wonder if store.each can got the deleted record?

mberrie
13 Sep 2011, 4:14 AM
There is no Store.reject() method in 4.0 (at least not in 4.0.2a).

See here (http://www.sencha.com/forum/showthread.php?136871-Where-did-rejectChanges%28%29-go) and here (http://www.sencha.com/forum/showthread.php?135716-Equivalent-of-Ext.data.Store.rejectChanges%28%29).

The provided workarounds don't consider deleted records though.

I suggest something like this (untested):



Ext.override(Ext.data.Store, {
rejectChanges: function() {
var me = this;

// re-add removed records
Ext.each(me.removed, function(rec) {
rec.join(me);
me.data.add(rec);
});
me.removed = [];

// revert dirty records and trash newly added records ('phantoms')
me.each(function(rec) {
if (rec.dirty) {
rec.reject();
}
if (rec.phantom) {
record.unjoin(me); // probably not really necessary
me.data.remove(rec);
}
});

me.fireEvent('datachanged', me);
}
});