Skip to content

Commit

Permalink
Merge pull request #3488 from bmac/async-default
Browse files Browse the repository at this point in the history
Relationships are async by default in 2.0
  • Loading branch information
igorT committed Jul 10, 2015
2 parents 2b4bf97 + 53c71a4 commit 19a1146
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 55 deletions.
13 changes: 1 addition & 12 deletions packages/ember-data/lib/system/relationships/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,19 @@ function belongsTo(modelName, options) {

opts = opts || {};

var shouldWarnAsync = false;
if (typeof opts.async === 'undefined') {
shouldWarnAsync = true;
}

var meta = {
type: userEnteredModelName,
isRelationship: true,
options: opts,
kind: 'belongsTo',
key: null,
shouldWarnAsync: shouldWarnAsync
key: null
};

return Ember.computed({
get: function(key) {
Ember.warn('You provided a serialize option on the "' + key + '" property in the "' + this._internalModel.modelName + '" class, this belongs in the serializer. See DS.Serializer and it\'s implementations http://emberjs.com/api/data/classes/DS.Serializer.html', !opts.hasOwnProperty('serialize'));
Ember.warn('You provided an embedded option on the "' + key + '" property in the "' + this._internalModel.modelName + '" class, this belongs in the serializer. See DS.EmbeddedRecordsMixin http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html', !opts.hasOwnProperty('embedded'));

if (meta.shouldWarnAsync) {
Ember.deprecate(`In Ember Data 2.0, relationships will be asynchronous by default. You must set \`${key}: DS.belongsTo('${modelName}', { async: false })\` if you wish for a relationship remain synchronous.`);
meta.shouldWarnAsycn = false;
}

return this._internalModel._relationships.get(key).getRecord();
},
set: function(key, value) {
Expand Down
12 changes: 1 addition & 11 deletions packages/ember-data/lib/system/relationships/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ function hasMany(type, options) {

options = options || {};

var shouldWarnAsync = false;
if (typeof options.async === 'undefined') {
shouldWarnAsync = true;
}

if (typeof type === 'string') {
type = normalizeModelName(type);
}
Expand All @@ -139,16 +134,11 @@ function hasMany(type, options) {
isRelationship: true,
options: options,
kind: 'hasMany',
key: null,
shouldWarnAsync: shouldWarnAsync
key: null
};

return Ember.computed({
get: function(key) {
if (meta.shouldWarnAsync) {
Ember.deprecate(`In Ember Data 2.0, relationships will be asynchronous by default. You must set \`${key}: DS.hasMany('${type}', { async: false })\` if you wish for a relationship remain synchronous.`);
meta.shouldWarnAsync = false;
}
var relationship = this._internalModel._relationships.get(key);
return relationship.getRecords();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import OrderedSet from "ember-data/system/ordered-set";

export default function Relationship(store, record, inverseKey, relationshipMeta) {
var async = relationshipMeta.options.async;
this.members = new OrderedSet();
this.canonicalMembers = new OrderedSet();
this.store = store;
this.key = relationshipMeta.key;
this.inverseKey = inverseKey;
this.record = record;
this.isAsync = relationshipMeta.options.async;
this.isAsync = typeof async === 'undefined' ? true : async;
this.relationshipMeta = relationshipMeta;
//This probably breaks for polymorphic relationship in complex scenarios, due to
//multiple possible modelNames
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/tests/integration/filter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module("integration/filter - DS.Model updating", {
name: 'Scumbag Bryn'
}
}];
Person = DS.Model.extend({ name: DS.attr('string'), bestFriend: DS.belongsTo('person', { inverse: null }) });
Person = DS.Model.extend({ name: DS.attr('string'), bestFriend: DS.belongsTo('person', { inverse: null, async: false }) });

env = setupStore({ person: Person });
store = env.store;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,7 @@ test("belongsTo gives a warning when provided with an embedded option", function
}, /You provided an embedded option on the "hobby" property in the "person" class, this belongs in the serializer. See DS.EmbeddedRecordsMixin/);
});

module("unit/model/relationships - DS.belongsTo async by default deprecations", {
setup: function() {
setupStore();
}
});

test("setting DS.belongsTo without async false triggers deprecation", function() {
test("DS.belongsTo should be async by default", function() {
var Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person', { async: false })
Expand All @@ -332,12 +326,10 @@ test("setting DS.belongsTo without async false triggers deprecation", function()
var env = setupStore({ tag: Tag, person: Person });
var store = env.store;

expectDeprecation(
function() {
run(function() {
store.createRecord('person').get('tag');
});
},
/In Ember Data 2.0, relationships will be asynchronous by default. You must set `tag: DS.belongsTo\('tag', { async: false }\)`/
);

run(function() {
var person = store.createRecord('person');

ok(person.get('tag') instanceof DS.PromiseObject, 'tag should be an async relationship');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,7 @@ test("it is possible to add an item to a relationship, remove it, then add it ag
equal(tags.objectAt(2), tag3);
});

module("unit/model/relationships - DS.hasMany async by default deprecations", {
setup: function() {
env = setupStore();
}
});

test("setting DS.hasMany without async false triggers deprecation", function() {
test("DS.hasMany is async by default", function() {
var Tag = DS.Model.extend({
name: DS.attr('string'),
people: DS.hasMany('person')
Expand All @@ -488,12 +482,8 @@ test("setting DS.hasMany without async false triggers deprecation", function() {
var env = setupStore({ tag: Tag, person: Person });
var store = env.store;

expectDeprecation(
function() {
run(function() {
store.createRecord('tag').get('people');
});
},
/In Ember Data 2.0, relationships will be asynchronous by default. You must set `people: DS.hasMany\('person', { async: false }\)/
);
run(function() {
var tag = store.createRecord('tag');
ok(tag.get('people') instanceof DS.PromiseArray, 'people should be an async relationship');
});
});

0 comments on commit 19a1146

Please sign in to comment.