PDA

View Full Version : Any way to determine if a Store is modified?



Gjslick
18 Oct 2012, 11:31 AM
Anyone know how to determine if a Store itself is modified? Meaning: if the store has had any models added to it, modified in it, or removed from it?

It seems I can determine the modified and removed models with getModifiedRecords() and getRemovedRecords(), but I can't determine any added models.

getNewRecords() seems to only return models added to the store which are "new" themselves (i.e. they are phantom records without an id). Can't seem to determine models added to the store which are *not* phantom records (i.e. have an id).

Also, there's no simple hasChanges() method to go along with commitChanges() and rejectChanges().

vietits
18 Oct 2012, 6:30 PM
Try this:


Ext.onReady(function(){
Ext.define('Override.data.Store', {
override: 'Ext.data.Store',
constructor: function(){
this.callParent(arguments);
this.setModified(false);
},
load: function(){
this.callParent(arguments);
this.setModified(false);
},
insert: function(){
this.callParent(arguments);
this.setModified(true);
},
afterEdit: function(){
this.callParent(arguments);
this.setModified(true);
},
remove: function(){
this.callParent(arguments);
this.setModified(true);
},
isModified: function(){
return this._modified;
},
setModified: function(flag){
this._modified = flag;
}
});

var store = Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
data: [{
id: 1, name: 'A'
},{
id: 2, name: 'B'
}]
});
console.log('load -> modified:', store.isModified());

store.setModified(false);
store.add({id: 3, name: 'C'});
console.log('add -> modified:', store.isModified());


store.setModified(false);
store.getAt(0).set({name: 'D'});
console.log('update -> modified:', store.isModified());


store.setModified(false);
store.removeAt(0);
console.log('remove -> modified:', store.isModified());
});