Permalink
Browse files

Model instances in model store will be updated when findAll or findOn…

…e retrieves updated instances fixes #40
  • Loading branch information...
ccummings
ccummings committed May 16, 2012
1 parent c036590 commit e4606906d37797d4ff551d1924d44f0c4d516fb7
Showing with 28 additions and 1 deletion.
  1. +1 −1 model/model.js
  2. +27 −0 model/model_test.js
View
@@ -735,7 +735,7 @@ steal('can/observe', function() {
if ( attributes instanceof this ) {
attributes = attributes.serialize();
}
- var model = this.store[attributes[this.id]] || new this( attributes );
+ var model = this.store[attributes[this.id]] ? this.store[attributes[this.id]].attr(attributes) : new this( attributes );
if(this._reqs){
this.store[attributes[this.id]] = model;
}
View
@@ -556,6 +556,33 @@ test("store ajax binding", function(){
})
+test("store instance updates", function(){
+ var Guy, updateCount;
+ Guy = can.Model({
+ findAll : 'GET /guys'
+ },{});
+ updateCount = 0;
+
+ can.fixture("GET /guys", function(){
+ var guys = [[{id: 1, updateCount: updateCount, nested: {count: updateCount}}]];
+ updateCount++;
+ return guys;
+ });
+ stop();
+ Guy.findAll({}, function(guys){
+ start();
+ guys[0].bind('updated', function(){});
+ ok(Guy.store[1], 'instance stored');
+ equals(Guy.store[1].updateCount, 0, 'updateCount is 0')
+ equals(Guy.store[1].nested.count, 0, 'nested.count is 0')
+ })
+ Guy.findAll({}, function(guys){
+ equals(Guy.store[1].updateCount, 1, 'updateCount is 1')
+ equals(Guy.store[1].nested.count, 1, 'nested.count is 1')
+ })
+
+})
+
test("templated destroy", function(){
var MyModel = can.Model({
destroy : "/destroyplace/{id}"

0 comments on commit e460690

Please sign in to comment.