Skip to content

Commit

Permalink
corrected bug when restoring more times than stored
Browse files Browse the repository at this point in the history
  • Loading branch information
Derick Bailey committed Sep 2, 2011
1 parent 4f6ba4c commit 85ac510
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
7 changes: 7 additions & 0 deletions backbone.memento.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Backbone.Memento = function(model){
function getRemovedAttrDiff(newAttrs, oldAttrs){
var removedAttrs = [];

// guard clause to ensure we have attrs to compare
if (!newAttrs || !oldAttrs){
return removedAttrs;
}

// if the attr is found in the old set but not in
// the new set, then it was remove in the new set
for (var attr in oldAttrs){
if (oldAttrs.hasOwnProperty(attr)){
if (!newAttrs.hasOwnProperty(attr)){
Expand Down
54 changes: 54 additions & 0 deletions spec/javascripts/clear.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe("clearing all mementos", function(){
beforeEach(function(){
this.model = new AModel();
});

describe("when clearing", function(){
it("should restore to first memento given successive save points", function(){
var changed = false;
this.model.set({foo: "bar"});
this.model.store();
this.model.set({foo: "baz"});
this.model.store();
this.model.set({foo: "qux"});
this.model.store();

this.model.bind("change:foo", function(){
changed = true;
});

expect(this.model.get('foo')).toBe('qux');
this.model.clear();
expect(changed).toBeTruthy();
expect(this.model.get('foo')).toBe('bar');
});

it("should lose all other save points", function(){
var changed = false;
this.model.set({foo: "bar"});
this.model.store();
this.model.set({foo: "baz"});
this.model.store();
this.model.set({foo: "qux"});
this.model.store();

this.model.clear();
this.model.restore();

expect(this.model.get('foo')).toBe('bar'); //should not be qux
});

it("should do nothing given no store point", function(){
var changed = false;
this.model.set({foo: "bar"});

this.model.bind("change:foo", function(){
changed = true;
});
this.model.clear();

expect(this.model.get('foo')).toBe('bar');
expect(changed).toBeFalsy();
});
});
});
18 changes: 14 additions & 4 deletions spec/javascripts/memento.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ describe("memento", function(){
});
});

describe("when mementoing once and restoring twice", function(){
beforeEach(function(){
this.model.set({foo: "bar"});
this.model.store();
});

it("should not restore anything", function(){
this.model.restore();
this.model.restore();
expect(this.model.get("foo")).toBe("bar");
});
});

describe("when mementoing twice and rolling back once", function(){
beforeEach(function(){
this.model.set({foo: "bar"});
Expand Down Expand Up @@ -84,8 +97,7 @@ describe("memento", function(){
expect(this.model.get('foo')).toBe('bar');
});


it("should lose all other save points", function(){
it("should lose all other save points", function(){
var changed = false;
this.model.set({foo: "bar"});
this.model.store();
Expand All @@ -100,8 +112,6 @@ describe("memento", function(){
expect(this.model.get('foo')).toBe('bar'); //should not be qux
});



it("should do nothing given no store point", function(){
var changed = false;
this.model.set({foo: "bar"});
Expand Down

0 comments on commit 85ac510

Please sign in to comment.