From 03aa1f6d1807af55bfcc55da4080d34d4d6da597 Mon Sep 17 00:00:00 2001 From: Daniil Pokrovsky Date: Mon, 16 Jun 2014 00:24:44 +0400 Subject: [PATCH] Correctly set isDirty in embedded objects after saving new record --- packages/ember-model/lib/model.js | 1 - .../ember-model/tests/dirty_tracking_test.js | 41 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/ember-model/lib/model.js b/packages/ember-model/lib/model.js index 6276b6f..9c20eae 100644 --- a/packages/ember-model/lib/model.js +++ b/packages/ember-model/lib/model.js @@ -251,7 +251,6 @@ Ember.Model = Ember.Object.extend(Ember.Evented, { set(this, 'isNew', false); - set(this, '_dirtyAttributes', []); this.constructor.addToRecordArrays(this); this.trigger('didCreateRecord'); this.didSaveRecord(); diff --git a/packages/ember-model/tests/dirty_tracking_test.js b/packages/ember-model/tests/dirty_tracking_test.js index 996f37a..3c6bb13 100644 --- a/packages/ember-model/tests/dirty_tracking_test.js +++ b/packages/ember-model/tests/dirty_tracking_test.js @@ -701,3 +701,44 @@ test("manipulating the content of objects in a hasMany should dirty the parent", ok(post.get('comments.isDirty') === true, "post.comments should be dirty after changing comment1's content"); ok(post.get('isDirty') === true, "Post should be dirty after changing comment1's content"); }); + +test("after saving new record, the model and it's embedded properties shouldn't be dirty", function() { + expect(5); + + var Comment = Ember.Model.extend({ + id: Ember.attr(), + name: Ember.attr() + }); + + var Post = Ember.Model.extend({ + id: Ember.attr(), + comments: Ember.hasMany(Comment, {key: 'comments', embedded: true}) + }); + + Post.adapter = { + createRecord: function(record) { + ok(true, "createRecord was called"); + var deferred = Ember.Deferred.create(); + deferred.then(function() { + record.didCreateRecord(); + }); + deferred.resolve(record); + return deferred; + } + }; + + var post = Post.create(); + post.get('comments').create({name: 'Comment 1'}); + + ok(post.get('isDirty') === true, 'Post should be dirty initially'); + ok(post.get('comments.isDirty') === true, 'Comments should be dirty initially'); + + stop(); + Ember.run(function() { + post.save().then(function() { + start(); + ok(!post.get('isDirty'), "Post should be clean"); + ok(!post.get('comments.isDirty'), "Comments in post should be clean"); + }); + }); +});