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 store.recordIsLoaded #4665

Merged
merged 2 commits into from
Nov 19, 2016
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
42 changes: 23 additions & 19 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,20 +1025,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 @@ -1750,27 +1761,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