Skip to content

Commit

Permalink
Deprecate complex objects as an attributes defaultValue.
Browse files Browse the repository at this point in the history
They often do not work how a user expects.
Closes #3766
  • Loading branch information
bmac committed Oct 14, 2015
1 parent 129a04d commit eabaa57
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"jQuery",
"expectAssertion",
"expectDeprecation",
"expectNoDeprecation",
"warns",
"noWarns",
"throws",
Expand Down
8 changes: 7 additions & 1 deletion packages/ember-data/lib/system/model/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ function getDefaultValue(record, options, key) {
if (typeof options.defaultValue === "function") {
return options.defaultValue.apply(null, arguments);
} else {
return options.defaultValue;
let defaultValue = options.defaultValue;
Ember.deprecate(`Non primative defaultValues are deprecated because they are shared between all instances. If you would like to use a complex object as a default value please provide a function that returns the complex object.`,
typeof defaultValue !== 'object' || defaultValue === null, {
id: 'ds.defaultValue.complex-object',
until: '3.0.0'
});
return defaultValue;
}
}

Expand Down
35 changes: 35 additions & 0 deletions packages/ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,41 @@ test("a defaultValue function gets the record, options, and key", function() {
get(tag, 'createdAt');
});

test("a complex object defaultValue is deprecated ", function() {
var Tag = DS.Model.extend({
tagInfo: DS.attr({ defaultValue: [] })
});
var tag;

var store = createStore({
tag: Tag
});

run(function() {
tag = store.createRecord('tag');
});
expectDeprecation(function() {
get(tag, 'tagInfo');
}, /Non primative defaultValues are deprecated/);
});

test("a null defaultValue is not deprecated", function() {
var Tag = DS.Model.extend({
tagInfo: DS.attr({ defaultValue: null })
});
var tag;

var store = createStore({
tag: Tag
});

run(function() {
tag = store.createRecord('tag');
});
expectNoDeprecation();
equal(get(tag, 'tagInfo'), null);
});

test("setting a property to undefined on a newly created record should not impact the current state", function() {
var Tag = DS.Model.extend({
name: DS.attr('string')
Expand Down

0 comments on commit eabaa57

Please sign in to comment.