DS.Store#findRecord()

findRecord (modelName, id, options) Promise

Defined in addon/-private/system/store.js:461
Available since 1.13.0

This method returns a record for a given type and id combination.

The findRecord method will always resolve its promise with the same object for a given type and id.

The findRecord method will always return a promise that will be resolved with the record.

Example

app/routes/post.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id);
  }
});

If the record is not yet available, the store will ask the adapter's find method to find the necessary data. If the record is already present in the store, it depends on the reload behavior when the returned promise resolves.

Reloading

The reload behavior is configured either via the passed options hash or the result of the adapter's shouldReloadRecord.

If { reload: true } is passed or adapter.shouldReloadRecord evaluates to true, then the returned promise resolves once the adapter returns data, regardless if the requested record is already in the store:

store.push({
  data: {
    id: 1,
    type: 'post',
    revision: 1
  }
});

// adapter#findRecord resolves with
// [
//   {
//     id: 1,
//     type: 'post',
//     revision: 2
//   }
// ]
store.findRecord('post', 1, { reload: true }).then(function(post) {
  post.get("revision"); // 2
});

If no reload is indicated via the abovementioned ways, then the promise immediately resolves with the cached version in the store.

Background Reloading

Optionally, if adapter.shouldBackgroundReloadRecord evaluates to true, then a background reload is started, which updates the records' data, once it is available:

// app/adapters/post.js
import ApplicationAdapter from "./application";

export default ApplicationAdapter.extend({
  shouldReloadRecord(store, snapshot) {
    return false;
  },

  shouldBackgroundReloadRecord(store, snapshot) {
    return true;
  }
});

// ...

store.push({
  data: {
    id: 1,
    type: 'post',
    revision: 1
  }
});

var blogPost = store.findRecord('post', 1).then(function(post) {
  post.get('revision'); // 1
});

// later, once adapter#findRecord resolved with
// [
//   {
//     id: 1,
//     type: 'post',
//     revision: 2
//   }
// ]

blogPost.get('revision'); // 2

If you would like to force or prevent background reloading, you can set a boolean value for backgroundReload in the options object for findRecord.

app/routes/post/edit.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id, { backgroundReload: false });
  }
});

If you pass an object on the adapterOptions property of the options argument it will be passed to you adapter via the snapshot

app/routes/post/edit.js
import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id, {
      adapterOptions: { subscribe: false }
    });
  }
});
app/adapters/post.js
import MyCustomAdapter from './custom-adapter';

export default MyCustomAdapter.extend({
  findRecord: function(store, type, id, snapshot) {
    if (snapshot.adapterOptions.subscribe) {
      // ...
    }
    // ...
  }
});

See peekRecord to get the cached version of a record.

Parameters:

modelName String
id (String|Integer)
options Object

Returns:

Promise
promise
doc_EmberJs
2016-11-30 16:50:51
Comments
Leave a Comment

Please login to continue.