Skip to content

Commit

Permalink
[BUGFIX release] Fix unconsistent behavior in Model.changedAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
cibernox committed Jul 24, 2015
1 parent 79de931 commit dff2f0b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Errors from "ember-data/system/model/errors";
*/

var get = Ember.get;
var merge = Ember.merge;
var copy = Ember.copy;

function intersection (array1, array2) {
var result = [];
Expand Down Expand Up @@ -611,7 +613,9 @@ var Model = Ember.Object.extend(Ember.Evented, {
*/
changedAttributes: function() {
var oldData = get(this._internalModel, '_data');
var newData = get(this._internalModel, '_attributes');
var currentData = get(this._internalModel, '_attributes');
var inFlightData = get(this._internalModel, '_inFlightAttributes');
var newData = merge(copy(inFlightData), currentData);
var diffData = Object.create(null);

var newDataKeys = Object.keys(newData);
Expand Down
63 changes: 62 additions & 1 deletion packages/ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ test("it should cache attributes", function() {
run(store, 'destroy');
});
});

});

test("changedAttributes() return correct values", function() {
Expand Down Expand Up @@ -298,6 +297,68 @@ test("changedAttributes() return correct values", function() {
equal(Object.keys(mascot.changedAttributes()).length, 0, 'after rollback attributes there are no changes');
});

test("changedAttributes() works while the record is being saved", function() {
var cat;
var adapter = DS.Adapter.extend({
createRecord(store, model, snapshot) {
deepEqual(cat.changedAttributes(), { name: [undefined, 'Argon'], likes: [undefined, 'Cheese'] });
return {};
}
});
var Mascot = DS.Model.extend({
name: DS.attr('string'),
likes: DS.attr('string'),
isMascot: DS.attr('boolean')
});

var store = createStore({
mascot: Mascot,
adapter: adapter
});

run(function() {
cat = store.createRecord('mascot');
cat.setProperties({ name: 'Argon', likes: 'Cheese' });
cat.save();
});
});

test("changedAttributes() works while re record is being updated", function() {
var cat;
var adapter = DS.Adapter.extend({
updateRecord(store, model, snapshot) {
deepEqual(cat.changedAttributes(), { name: ['Argon', 'Helia'], likes: ['Cheese', 'Mussels'] });
return { id: '1', type: 'mascot' };
}
});
var Mascot = DS.Model.extend({
name: DS.attr('string'),
likes: DS.attr('string'),
isMascot: DS.attr('boolean')
});

var store = createStore({
mascot: Mascot,
adapter: adapter
});

run(function() {
store.push({
data: {
type: 'mascot',
id: '1',
attributes: {
name: 'Argon',
likes: 'Cheese'
}
}
});
cat = store.peekRecord('mascot', 1);
cat.setProperties({ name: 'Helia', likes: 'Mussels' });
cat.save();
});
});

test("a DS.Model does not require an attribute type", function() {
var Tag = DS.Model.extend({
name: DS.attr()
Expand Down

0 comments on commit dff2f0b

Please sign in to comment.