angular2-jsonapi
angular2-jsonapi copied to clipboard
rollbackAttributes() only rolls back one change
Hi,
I have a use case where I need to rollback all attributes of a model to their last saved state. In the code of the JsonApiModel I found the method rollbackAttributes()
which I excpected to do the job. But when doing two changes on a model attribute it only rolls back one change and not two changes.
Example:
let test = this.datastoreService.createRecord(Role, {});
test.name = "test";
test.save();
console.log(test.name); // prints "test"
test.name = "test2";
test.name = "test22";
test.rollbackAttributes();
console.log(test.name); //prints "test2" but I expected it to print "test"
Is this a bug or expected behaviour? If it's a bug I would like to propose a fix in the saveAnnotations()
method of the Attribute decorator and make a pull request.
I second this. This is unexpected behaviour as the documentation says:
You can tell if a record has outstanding changes that have not yet been saved by checking its hasDirtyAttributes property.
At this point, you can either persist your changes via save() or you can roll back your changes. Calling rollbackAttributes() for a saved record reverts all the dirty attributes to their original value.
By 'their original value' I understand the value it had the last time save() was called, or when freshly loaded from the database.
Are you still interested in this change? In general I am not sure if this feature should be part of the library at all, I would prefer that the library focuses just on the mapping/serialization part.
If it's not something the library is going to support, don't advertise support for it and don't leave it partially implemented+broken.
It is a feature I'd like to have in whatever client-side data querying/(de)serialization/caching/management library replaces the one I'm currently using.