Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new record which is marked as invalid can be rollbacked #3083

Merged
merged 1 commit into from
Jun 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ var createdState = dirtyState({
isNew: true
});

createdState.invalid.rolledBack = function(record) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much better solution then my last commit. I was trying to handle it same way but within createdState.invalid.rolleback which is of course wrong. +1

record.transitionTo('deleted.saved');
};
createdState.uncommitted.rolledBack = function(record) {
record.transitionTo('deleted.saved');
};
Expand Down
45 changes: 45 additions & 0 deletions packages/ember-data/tests/unit/model/rollback-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,51 @@ test("new record can be rollbacked", function() {
equal(person.get('isDeleted'), true, "must be deleted");
});

test("invalid new record can be rollbacked", function() {
var person;
var adapter = DS.RESTAdapter.extend({
ajax: function(url, type, hash) {
var adapter = this;

return new Ember.RSVP.Promise(function(resolve, reject) {
/* If InvalidError is passed back in the reject it will throw the
exception which will bubble up the call stack (crashing the test)
instead of hitting the failure route of the promise.
So wrapping the reject in an Ember.run.next makes it so save
completes without failure and the failure hits the failure route
of the promise instead of crashing the save. */
Ember.run.next(function() {
reject(adapter.ajaxError({ name: 'is invalid' }));
});
});
},

ajaxError: function(jqXHR) {
return new DS.InvalidError(jqXHR);
}
});

env = setupStore({ person: Person, adapter: adapter });

run(function() {
person = env.store.createRecord('person', { id: 1 });
});

equal(person.get('isNew'), true, "must be new");
equal(person.get('isDirty'), true, "must be dirty");

run(function() {
person.save().then(null, async(function() {
equal(person.get('isValid'), false);
person.rollback();

equal(person.get('isNew'), false, "must not be new");
equal(person.get('isDirty'), false, "must not be dirty");
equal(person.get('isDeleted'), true, "must be deleted");
}));
});
});

test("deleted record can be rollbacked", function() {
var person, people;

Expand Down