Skip to content

Commit

Permalink
Merge pull request #4665 from runspired/deprecate-recordIsLoaded
Browse files Browse the repository at this point in the history
Deprecate store.recordIsLoaded
  • Loading branch information
fivetanley authored Nov 19, 2016
2 parents cebcbed + 84961dc commit fdc4b2d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
42 changes: 23 additions & 19 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,20 +1032,31 @@ Store = Service.extend({
},

/**
Returns true if a record for a given type and ID is already loaded.
This method returns true if a record for a given modelName and id is already
loaded in the store. Use this function to know beforehand if a findRecord()
will result in a request or that it will be a cache hit.
Example
```javascript
store.hasRecordForId('post', 1); // false
store.findRecord('post', 1).then(function() {
store.hasRecordForId('post', 1); // true
});
```
@method hasRecordForId
@param {(String|DS.Model)} modelName
@param {(String|Integer)} inputId
@param {(String|Integer)} id
@return {Boolean}
*/
hasRecordForId(modelName, inputId) {
hasRecordForId(modelName, id) {
assert("You need to pass a model name to the store's hasRecordForId method", isPresent(modelName));
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ inspect(modelName), typeof modelName === 'string');

let id = coerceId(inputId);
let trueId = coerceId(id);
let modelClass = this.modelFor(modelName);
let internalModel = this.typeMapFor(modelClass).idToRecord[id];
let internalModel = this.typeMapFor(modelClass).idToRecord[trueId];

return !!internalModel && internalModel.isLoaded();
},
Expand Down Expand Up @@ -1757,27 +1768,20 @@ Store = Service.extend({
},

/**
This method returns if a certain record is already loaded
in the store. Use this function to know beforehand if a findRecord()
will result in a request or that it will be a cache hit.
Example
```javascript
store.recordIsLoaded('post', 1); // false
store.findRecord('post', 1).then(function() {
store.recordIsLoaded('post', 1); // true
});
```
This method has been deprecated and is an alias for store.hasRecordForId, which should
be used instead.
@deprecated
@method recordIsLoaded
@param {String} modelName
@param {string} id
@return {boolean}
*/
recordIsLoaded(modelName, id) {
assert("You need to pass a model name to the store's recordIsLoaded method", isPresent(modelName));
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ inspect(modelName), typeof modelName === 'string');
deprecate(`Use of recordIsLoaded is deprecated, use hasRecordForId instead.`, {
id: 'ds.store.recordIsLoaded',
until: '3.0'
});
return this.hasRecordForId(modelName, id);
},

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,10 @@ module("unit/model - with a simple Person model", {
});

test("can ask if record with a given id is loaded", function(assert) {
assert.equal(store.recordIsLoaded('person', 1), true, 'should have person with id 1');
assert.equal(store.recordIsLoaded('person', 1), true, 'should have person with id 1');
assert.equal(store.recordIsLoaded('person', 4), false, 'should not have person with id 4');
assert.equal(store.recordIsLoaded('person', 4), false, 'should not have person with id 4');
assert.equal(store.hasRecordForId('person', 1), true, 'should have person with id 1');
assert.equal(store.hasRecordForId('person', 1), true, 'should have person with id 1');
assert.equal(store.hasRecordForId('person', 4), false, 'should not have person with id 4');
assert.equal(store.hasRecordForId('person', 4), false, 'should not have person with id 4');
});

test("a listener can be added to a record", function(assert) {
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/store/asserts-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ const MODEL_NAME_METHODS = [
'findAll',
'peekAll',
'filter',
'recordIsLoaded',
'modelFor',
'modelFactoryFor',
'normalize',
'adapterFor',
'serializerFor'
];

testInDebug("Calling Store methods with no type asserts", function(assert) {
testInDebug("Calling Store methods with no modelName asserts", function(assert) {
assert.expect(MODEL_NAME_METHODS.length);
let store = createStore();

Expand Down

0 comments on commit fdc4b2d

Please sign in to comment.