Skip to content

Commit

Permalink
Rename rollback() to rollbackAttributes() for Model and InternalModel
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicEric committed Jun 12, 2015
1 parent 354c6d7 commit f69a9eb
Show file tree
Hide file tree
Showing 12 changed files with 473 additions and 415 deletions.
4 changes: 2 additions & 2 deletions packages/ember-data/lib/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ InternalModel.prototype = {
}
},

rollback: function() {
rollbackAttributes: function() {
var dirtyKeys = Ember.keys(this._attributes);

this._attributes = Ember.create(null);
Expand Down Expand Up @@ -669,7 +669,7 @@ InternalModel.prototype = {
Ember Data has 3 buckets for storing the value of an attribute on an internalModel.
`_data` holds all of the attributes that have been acknowledged by
a backend via the adapter. When rollback is called on a model all
a backend via the adapter. When rollbackAttributes is called on a model all
attributes will revert to the record's state in `_data`.
`_attributes` holds any change the user has made to an attribute
Expand Down
30 changes: 26 additions & 4 deletions packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ var Model = Ember.Object.extend(Ember.Evented, {
/**
Marks the record as deleted but does not save it. You must call
`save` afterwards if you want to persist it. You might use this
method if you want to allow the user to still `rollback()` a
delete after it was made.
method if you want to allow the user to still `rollbackAttributes()`
after a delete it was made.
Example
Expand All @@ -486,7 +486,7 @@ var Model = Ember.Object.extend(Ember.Evented, {
this.controller.get('model').save();
},
undo: function() {
this.controller.get('model').rollback();
this.controller.get('model').rollbackAttributes();
}
}
});
Expand Down Expand Up @@ -621,9 +621,31 @@ var Model = Ember.Object.extend(Ember.Evented, {
```
@method rollback
@deprecated Use `addAttributes()` instead
*/
rollback: function() {
this._internalModel.rollback();
Ember.deprecate('Using model.rollback() has been deprecated. Use model.rollbackAttributes() to discard any unsaved changes to a model.');
this.rollbackAttributes();
},

/**
If the model `isDirty` this function will discard any unsaved
changes. If the model `isNew` it will be removed from the store.
Example
```javascript
record.get('name'); // 'Untitled Document'
record.set('name', 'Doc 1');
record.get('name'); // 'Doc 1'
record.rollbackAttributes();
record.get('name'); // 'Untitled Document'
```
@method rollbackAttributes
*/
rollbackAttributes: function() {
this._internalModel.rollbackAttributes();
},

/*
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ var DirtyState = {
},

rollback: function(internalModel) {
internalModel.rollback();
internalModel.rollbackAttributes();
internalModel.triggerLater('ready');
}
},
Expand Down Expand Up @@ -632,7 +632,7 @@ var RootState = {
},

rollback: function(internalModel) {
internalModel.rollback();
internalModel.rollbackAttributes();
internalModel.triggerLater('ready');
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test("when deleted records are rolled back, they are still in their previous rec

run(function() {
jaime.deleteRecord();
jaime.rollback();
jaime.rollbackAttributes();
});
equal(all.get('length'), 2, 'record was not removed');
equal(filtered.get('length'), 2, 'record was not removed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ test("A sync belongsTo errors out if the record is unlaoded", function() {
}, /You looked up the 'user' relationship on a 'message' with id 1 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async \(`DS.belongsTo\({ async: true }\)`\)/);
});

test("Rollbacking a deleted record restores implicit relationship - async", function () {
test("Rollbacking attributes for a deleted record restores implicit relationship - async", function () {
Book.reopen({
author: DS.belongsTo('author', { async: true })
});
Expand All @@ -592,24 +592,24 @@ test("Rollbacking a deleted record restores implicit relationship - async", func
});
run(function() {
author.deleteRecord();
author.rollback();
author.rollbackAttributes();
book.get('author').then(function(fetchedAuthor) {
equal(fetchedAuthor, author, 'Book has an author after rollback');
equal(fetchedAuthor, author, 'Book has an author after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship - sync", function () {
test("Rollbacking attributes for a deleted record restores implicit relationship - sync", function () {
var book, author;
run(function() {
book = env.store.push('book', { id: 1, name: "Stanley's Amazing Adventures", author: 2 });
author = env.store.push('author', { id: 2, name: 'Stanley' });
});
run(function() {
author.deleteRecord();
author.rollback();
author.rollbackAttributes();
});
equal(book.get('author'), author, 'Book has an author after rollback');
equal(book.get('author'), author, 'Book has an author after rollback attributes');
});

test("Passing a model as type to belongsTo should not work", function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1060,39 +1060,39 @@ test("If reordered hasMany data has been pushed to the store, the many array ref
deepEqual(post.get('comments').toArray(), [comment4, comment2, comment3, comment1], 'Updated ordering is correct');
});

test("Rollbacking a deleted record restores implicit relationship correctly when the hasMany side has been deleted - async", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - async", function () {
var book, chapter;
run(function() {
book = env.store.push('book', { id: 1, title: "Stanley's Amazing Adventures", chapters: [2] });
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
book.get('chapters').then(function(fetchedChapters) {
equal(fetchedChapters.objectAt(0), chapter, 'Book has a chapter after rollback');
equal(fetchedChapters.objectAt(0), chapter, 'Book has a chapter after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the hasMany side has been deleted - sync", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the hasMany side has been deleted - sync", function () {
var book, chapter;
run(function() {
book = env.store.push('book', { id: 1, title: "Stanley's Amazing Adventures", chapters: [2] });
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
equal(book.get('chapters.firstObject'), chapter, "Book has a chapter after rollback");
equal(book.get('chapters.firstObject'), chapter, "Book has a chapter after rollback attributes");
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the belongsTo side has been deleted - async", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the belongsTo side has been deleted - async", function () {
Page.reopen({
chapter: DS.belongsTo('chapter', { async: true })
});
Expand All @@ -1103,27 +1103,27 @@ test("Rollbacking a deleted record restores implicit relationship correctly when
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
page.get('chapter').then(function(fetchedChapter) {
equal(fetchedChapter, chapter, 'Page has a chapter after rollback');
equal(fetchedChapter, chapter, 'Page has a chapter after rollback attributes');
});
});
});

test("Rollbacking a deleted record restores implicit relationship correctly when the belongsTo side has been deleted - sync", function () {
test("Rollbacking attributes for deleted record restores implicit relationship correctly when the belongsTo side has been deleted - sync", function () {
var chapter, page;
run(function() {
chapter = env.store.push('chapter', { id: 2, title: 'Sailing the Seven Seas' });
page = env.store.push('page', { id: 3, number: 1, chapter: 2 });
});
run(function() {
chapter.deleteRecord();
chapter.rollback();
chapter.rollbackAttributes();
});
run(function() {
equal(page.get('chapter'), chapter, "Page has a chapter after rollback");
equal(page.get('chapter'), chapter, "Page has a chapter after rollback attributes");
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});

/*
Rollback tests
Rollback Attributes tests
*/

test("Rollbacking a deleted record that has a ManyToMany relationship works correctly - async", function () {
test("Rollbacking attributes for a deleted record that has a ManyToMany relationship works correctly - async", function () {
var user, topic;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley', topics: [2] });
topic = store.push('topic', { id: 2, title: 'EmberFest was great' });
});
run(function() {
topic.deleteRecord();
topic.rollback();
topic.rollbackAttributes();
});
run(function() {
topic.get('users').then(async(function(fetchedUsers) {
Expand All @@ -235,13 +235,13 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});
run(function() {
account.deleteRecord();
account.rollback();
account.rollbackAttributes();
});
equal(account.get('users.length'), 1, 'Users are still there');
equal(user.get('accounts.length'), 1, 'Account got rolledback correctly into the user');
});

test("Rollbacking a created record that has a ManyToMany relationship works correctly - async", function () {
test("Rollbacking attributes for a created record that has a ManyToMany relationship works correctly - async", function () {
var user, topic;
run(function() {
user = store.push('user', { id: 1, name: 'Stanley' });
Expand All @@ -250,7 +250,7 @@ test("Rollbacking a created record that has a ManyToMany relationship works corr
run(function() {
user.get('topics').then(async(function(fetchedTopics) {
fetchedTopics.pushObject(topic);
topic.rollback();
topic.rollbackAttributes();
topic.get('users').then(async(function(fetchedUsers) {
equal(fetchedUsers.get('length'), 0, 'Users got removed');
equal(fetchedUsers.objectAt(0), null, "User can't be fetched");
Expand All @@ -271,7 +271,7 @@ test("Deleting a record that has a hasMany relationship removes it from the othe
});
run(function() {
account.get('users').pushObject(user);
user.rollback();
user.rollbackAttributes();
});
equal(account.get('users.length'), 0, 'Users got removed');
equal(user.get('accounts.length'), undefined, 'Accounts got rolledback correctly');
Expand Down
Loading

0 comments on commit f69a9eb

Please sign in to comment.