findAll (modelName, options) Promise
findAll
asks the adapter's findAll
method to find the records for the given type, and returns a promise which will resolve with all records of this type present in the store, even if the adapter only returns a subset of them.
app/routes/authors.js
import Ember from 'ember'; export default Ember.Route.extend({ model: function(params) { return this.store.findAll('author'); } });
When the returned promise resolves depends on the reload behavior, configured via the passed options
hash and the result of the adapter's shouldReloadAll
method.
Reloading
If { reload: true }
is passed or adapter.shouldReloadAll
evaluates to true
, then the returned promise resolves once the adapter returns data, regardless if there are already records in the store:
store.push({ data: { id: 'first', type: 'author' } }); // adapter#findAll resolves with // [ // { // id: 'second', // type: 'author' // } // ] store.findAll('author', { reload: true }).then(function(authors) { authors.getEach("id"); // ['first', 'second'] });
If no reload is indicated via the abovementioned ways, then the promise immediately resolves with all the records currently loaded in the store.
Background Reloading
Optionally, if adapter.shouldBackgroundReloadAll
evaluates to true
, then a background reload is started. Once this resolves, the array with which the promise resolves, is updated automatically so it contains all the records in the store:
// app/adapters/application.js export default DS.Adapter.extend({ shouldReloadAll(store, snapshotsArray) { return false; }, shouldBackgroundReloadAll(store, snapshotsArray) { return true; } }); // ... store.push({ data: { id: 'first', type: 'author' } }); var allAuthors; store.findAll('author').then(function(authors) { authors.getEach('id'); // ['first'] allAuthors = authors; }); // later, once adapter#findAll resolved with // [ // { // id: 'second', // type: 'author' // } // ] allAuthors.getEach('id'); // ['first', 'second']
If you would like to force or prevent background reloading, you can set a boolean value for backgroundReload
in the options object for findAll
.
app/routes/post/edit.js
import Ember from 'ember'; export default Ember.Route.extend({ model: function() { return this.store.findAll('post', { backgroundReload: false }); } });
If you pass an object on the adapterOptions
property of the options argument it will be passed to you adapter via the snapshotRecordArray
app/routes/posts.js
import Ember from 'ember'; export default Ember.Route.extend({ model: function(params) { return this.store.findAll('post', { adapterOptions: { subscribe: false } }); } });
app/adapters/post.js
import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ findAll: function(store, type, sinceToken, snapshotRecordArray) { if (snapshotRecordArray.adapterOptions.subscribe) { // ... } // ... } });
See peekAll to get an array of current records in the store, without waiting until a reload is finished.
See query to only get a subset of records from the server.
Parameters:
-
modelName
String
-
options
Object
Returns:
-
Promise
- promise
Please login to continue.