Skip to content

Commit

Permalink
Merge pull request #3280 from HeroicEric/bmac-deprecate-preload
Browse files Browse the repository at this point in the history
[WIP] Deprecate preload arg to `find`/`fetchById` & move in into the options preload key
  • Loading branch information
igorT committed Jun 10, 2015
2 parents 60c01a3 + f6d6263 commit 3d7dba5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 20 deletions.
53 changes: 39 additions & 14 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ Store = Service.extend({
without fetching the post you can pass in the post to the `find` call:
```javascript
store.find('comment', 2, { post: 1 });
store.find('comment', 2, { preload: { post: 1 } });
```
If you have access to the post model you can also pass the model itself:
Expand Down Expand Up @@ -476,7 +476,7 @@ Store = Service.extend({
@method find
@param {String} modelName
@param {(Object|String|Integer|null)} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@param {Object} options
@return {Promise} promise
*/
find: function(modelName, id, preload) {
Expand All @@ -494,8 +494,8 @@ Store = Service.extend({
Ember.deprecate('Calling store.find() with a query object is deprecated. Use store.query() instead.');
return this.query(modelName, id);
}

return this.findRecord(modelName, coerceId(id), preload);
var options = deprecatePreload(preload, this.modelFor(modelName), 'find');
return this.findRecord(modelName, coerceId(id), options);
},

/**
Expand Down Expand Up @@ -523,15 +523,16 @@ Store = Service.extend({
@method fetchById
@param {String} modelName
@param {(String|Integer)} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@param {Object} options
@return {Promise} promise
*/
fetchById: function(modelName, id, preload) {
Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var options = deprecatePreload(preload, this.modelFor(modelName), 'fetchById');
if (this.hasRecordForId(modelName, id)) {
return this.getById(modelName, id).reload();
} else {
return this.find(modelName, id, preload);
return this.findRecord(modelName, id, options);
}
},

Expand Down Expand Up @@ -569,12 +570,13 @@ Store = Service.extend({
@private
@param {String} modelName
@param {(String|Integer)} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@param {Object} options
@return {Promise} promise
*/
findById: function(modelName, id, preload) {
Ember.deprecate('Using store.findById() has been deprecated. Use store.findRecord() to return a record for a given type and id combination.');
return this.findRecord(modelName, id, preload);
var options = deprecatePreload(preload, this.modelFor(modelName), 'findById');
return this.findRecord(modelName, id, options);
},

/**
Expand All @@ -583,21 +585,23 @@ Store = Service.extend({
@method findRecord
@param {String} modelName
@param {(String|Integer)} id
@param {Object} preload - optional set of attributes and relationships passed in either as IDs or as actual models
@param {Object} options
@return {Promise} promise
*/
findRecord: function(modelName, id, preload) {
findRecord: function(modelName, id, options) {
Ember.assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var internalModel = this._internalModelForId(modelName, id);

return this._findByInternalModel(internalModel, preload);
return this._findByInternalModel(internalModel, options);
},

_findByInternalModel: function(internalModel, preload) {
_findByInternalModel: function(internalModel, options) {
var fetchedInternalModel;
options = options || {};


if (preload) {
internalModel._preloadData(preload);
if (options.preload) {
internalModel._preloadData(options.preload);
}

if (internalModel.isEmpty()) {
Expand Down Expand Up @@ -2172,5 +2176,26 @@ function setupRelationships(store, record, data) {
});
}

function deprecatePreload(preloadOrOptions, type, methodName) {
if (preloadOrOptions) {
var modelProperties = [];
var fields = Ember.get(type, 'fields');
fields.forEach(function(fieldType, key) {
modelProperties.push(key);
});
var preloadDetected = modelProperties.reduce(function(memo, key) {
return typeof preloadOrOptions[key] !== 'undefined' || memo;
}, false);
if (preloadDetected) {
Ember.deprecate(`Passing a preload argument to \`store.${methodName}\` is deprecated. Please move it to the preload key on the ${methodName} \`options\` argument.`);
var preload = preloadOrOptions;
return {
preload: preload
};
}
}
return preloadOrOptions;
}

export { Store };
export default Store;
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ test('buildURL - buildURL takes a record from find', function() {
});

run(function() {
store.find('comment', 1, { post: post }).then(async(function(post) {
store.find('comment', 1, { preload: { post: post } }).then(async(function(post) {
equal(passedUrl, "/posts/2/comments/1");
}));
});
Expand Down
96 changes: 91 additions & 5 deletions packages/ember-data/tests/unit/store/adapter-interop-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ test("initial values of attributes can be passed in as the third argument to fin
});

run(function() {
store.find('test', 1, { name: 'Test' });
store.find('test', 1, { preload: { name: 'Test' } });
});
});

Expand Down Expand Up @@ -419,7 +419,7 @@ test("initial values of belongsTo can be passed in as the third argument to find

run(function() {
tom = store.push('person', { id: 2, name: 'Tom' });
store.find('person', 1, { friend: tom });
store.find('person', 1, { preload: { friend: tom } });
});
});

Expand All @@ -445,7 +445,7 @@ test("initial values of belongsTo can be passed in as the third argument to find
env.registry.register('model:person', Person);

run(function() {
store.find('person', 1, { friend: 2 }).then(async(function() {
store.find('person', 1, { preload: { friend: 2 } }).then(async(function() {
store.getById('person', 1).get('friend').then(async(function(friend) {
equal(friend.get('id'), '2', 'Preloaded belongsTo set');
}));
Expand Down Expand Up @@ -477,7 +477,7 @@ test("initial values of hasMany can be passed in as the third argument to find a

run(function() {
tom = store.push('person', { id: 2, name: 'Tom' });
store.find('person', 1, { friends: [tom] });
store.find('person', 1, { preload: { friends: [tom] } });
});
});

Expand All @@ -504,7 +504,7 @@ test("initial values of hasMany can be passed in as the third argument to find a
env.registry.register('model:person', Person);

run(function() {
store.find('person', 1, { friends: [2] });
store.find('person', 1, { preload: { friends: [2] } });
});
});

Expand Down Expand Up @@ -814,3 +814,89 @@ test("store.fetchRecord reject records that were not found, even when those requ
});
}, /expected to find records with the following ids in the adapter response but they were missing/);
});

module("unit/store/adapter_interop - find preload deprecations", {
setup: function() {
var Person = DS.Model.extend({
name: DS.attr('string')
});

var TestAdapter = DS.Adapter.extend({
find: function(store, type, id, snapshot) {
equal(snapshot.attr('name'), 'Tom');
return Ember.RSVP.resolve({ id: id });
}
});

store = createStore({
adapter: TestAdapter,
person: Person
});
},
teardown: function() {
run(function() {
if (store) { store.destroy(); }
});
}
});

test("store#find with deprecated preload passes correct options to store#findRecord", function() {
expect(2);

var expectedOptions = { preload: { name: 'Tom' } };

store.reopen({
findRecord: function(modelName, id, options) {
deepEqual(options, expectedOptions,
'deprecated preload transformed to new options store#findRecord');
}
});

expectDeprecation(
function() {
run(function() {
store.find('person', 1, { name: 'Tom' });
});
},
/Passing a preload argument to `store.find` is deprecated./
);
});

test("Using store#find with preload is deprecated", function() {
expect(2);

expectDeprecation(
function() {
run(function() {
store.find('person', 1, { name: 'Tom' });
});
},
/Passing a preload argument to `store.find` is deprecated./
);
});

test("Using store#fetchById with preload is deprecated", function() {
expect(2);

expectDeprecation(
function() {
run(function() {
store.fetchById('person', 1, { name: 'Tom' });
});
},
/Passing a preload argument to `store.fetchById` is deprecated./
);
});

test("Using store#findById with preload is deprecated", function() {
expect(2);

expectDeprecation(
function() {
run(function() {
store.findById('person', 1, { name: 'Tom' });
});
},
/Passing a preload argument to `store.findById` is deprecated/
);
});

0 comments on commit 3d7dba5

Please sign in to comment.