Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate complex objects as an attributes defaultValue. #3855

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 primitive 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 primitive 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