PDA

View Full Version : [FIXED] Where did rejectChanges() go?



mother
13 Jun 2011, 9:06 AM
Hi all,

I'm going crazy on this one - with 3.x I was able to issue rejectChanges() on a data store, and all edited values in a grid would be 'reset' to their original values. An example is found here:

http://gridsearch.extjs.eu/

Which is the way to do this with 4.x? I've been through the methods of Ext.data.Store, with no equivalent to be found, and only in Ext.data.Model can we find reject() which:


Rejects all changes made to the model instance since either creation, or the last commit operation. Modified fields are reverted to their original values.Trouble is I've not found a way to successfully call it. The docs state:


Usually called by the Ext.data.Store (http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Store) to which this model instance has been joined (http://docs.sencha.com/ext-js/4-0/#/api/Ext.data.Model-method-join).The bottom line is: all the documents, examples, forum posts, etc. I have found all reference methods that seem no longer supported. The goal is simple, after someone makes edits on a grid, and the dirty marks appear, I need a button to undo the changes and restore original values.

Regards,

Mike

jsakalos
13 Jun 2011, 10:49 AM
Mystery! I cannot find it either. And I cannot find getModifiedRecords (or an equivalent) too. My first idea was to get all modified records (models) and call reject on all of them.

There is still a workaround: loop through all records of store, check the dirty flag and call reject on all of them. Or unconditionally on all records in loop.

In any case, this is clumsy and such utility methods should be part of the store's code, therefore, I'm moving this thread to bugs for devels to know about it.

mother
13 Jun 2011, 10:54 AM
Thanks for your reply, and for confirming I was not going insane :)

Indeed, the following works:


store.each(function(r){
r.reject();
});Cheers,

Mike

elilindner
9 Aug 2011, 5:32 PM
I'm sure it isn't the best method, but I use this for the time being until it's back in the core



Ext.override(Ext.data.Store, {
rejectChanges: function() {
this.each(function(rec) {
if (rec.dirty) {
rec.reject();
}
if (rec.phantom) {
this.remove(rec);
}
}, this);
}
});

mberrie
13 Sep 2011, 4:17 AM
What about deleted records?

This is 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);
}
});

shubakk
6 Oct 2011, 10:43 PM
I had some troubles with mberrie's snippet while working with a filtered store.
You might want to handle the snapshot array :



rejectChanges : function(){

Ext.each(this.removed, function(rec) {
rec.join(this);
this.data.add(rec);
if(Ext.isDefined(this.snapshot)){
this.snapshot.add(rec);
}
},this);
this.removed = [];

this.each(function(rec) {

if (rec.dirty === true) {
rec.reject();
}

if (rec.phantom === true) {
rec.unjoin(this);
this.data.remove(rec);
if(Ext.isDefined(this.snapshot)){
this.snapshot.remove(rec);
}
}
},this);
this.fireEvent('datachanged', this);
}

mdlincoln
7 Oct 2011, 11:38 AM
Good news, we have decided to log this as missing functionality in ExtJS 4 so that it will receive a priority review by engineering.

brian.moeskau
22 Nov 2011, 11:52 AM
This will be fixed in 4.1. It looks like this:


rejectChanges : function() {
var me = this,
recs = me.getModifiedRecords(),
len = recs.length,
i = 0;

for (; i < len; i++) {
recs[i].reject();
if (recs[i].phantom) {
me.remove(recs[i]);
}
}

recs = me.removed;
len = recs.length;

for (i = 0; i < len; i++) {
me.insert(recs[i].lastIndex || 0, recs[i]);
recs[i].reject();
// lastIndex will get re-added if this rec gets removed again later
delete recs[i].lastIndex;
}

// Since removals are cached in a simple array we can simply reset it here.
// Adds and updates are managed in the data MixedCollection and should already be current.
me.removed.length = 0;
}


Please test it out and let me know if you see any issues.

Phantom
23 Nov 2011, 6:26 AM
This will be fixed in 4.1. It looks like this:


rejectChanges : function() {
var me = this,
recs = me.getModifiedRecords(),
...
}



As far as i can see from the docs, there must be getUpdatedRecords instead in Ext4, isn't it?
Because getModifiedRecords() is missing in Ext 4.0.7

Phantom
23 Nov 2011, 6:33 AM
Also, what is about commitChanges() function which seems missing too?

brian.moeskau
23 Nov 2011, 7:45 AM
Sorry, this was added too:


getModifiedRecords : function(){
return [].concat(this.getNewRecords(), this.getUpdatedRecords());
}


Not sure about commitChanges, will have to look into that. It should probably be reinstated as well I assume.

nt3rp
11 Jan 2012, 8:21 AM
I was just taking a look at this patch when working on a project, and I noticed that the 'rejectChanges' function references the 'lastIndex' property of a record, which, as far as I have seen, is not included elsewhere in the code.

I found that patching the 'remove' function in the store solves this problem:



/**
* Overridden because rejectChanges expects lastIndex to have been set on the record when it is deleted.
*/
remove: function (records, /* private */ isMove) {
if (!Ext.isArray(records)) {
records = [records];
}


/*
* Pass the isMove parameter if we know we're going to be re-inserting this record
*/
isMove = isMove === true;
var me = this,
sync = false,
i,
length = records.length,
isPhantom,
index,
record;


for (i = 0; i < length; i++) {
record = records[i];
index = me.data.indexOf(record);


if (me.snapshot) {
me.snapshot.remove(record);
}


if (index > -1) {
isPhantom = record.phantom === true;
if (!isMove && !isPhantom) {
// don't push phantom records onto removed
me.removed.push(record);
}


record.unjoin(me);
//--------FIX HERE----------
record.lastIndex = index;
//--------/FIX HERE----------
me.data.remove(record);
sync = sync || !isPhantom;


me.fireEvent('remove', me, record, index);
}
}


me.fireEvent('datachanged', me);
if (!isMove && me.autoSync && sync) {
me.sync();
}
}

StijnDeWitt
12 Jul 2013, 12:23 AM
Also, what is about commitChanges() function which seems missing too?


I think they have synch() for that now...