I’m using ember-data-1.0.0-beta.3. All works fine, but I see a network request every transition, even when I see data in the store using the chrome plugin.
findAll does a query, which queries for data from the API. If you want to get the data that’s already in the store, then you should filter. You can read more about filtering vs querying in the guide.
find also makes a query call. I’m not sure, but I think that to get data from memory, you have explicitly ask for it by doing a filter. You’ll have to take into account the scenario where the item is not actually in memory.
You can use store.all('user') to get the already loaded data:
App.ApplicationRoute = Ember.Route.extend({
model: function() {
// load all user data once, when app load
return this.find('user');
}
})
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://api.myapp.dev'
});
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});
App.UsersRoute = Em.Route.extend({
model: function() {
// because the data is already loaded by ApplicationRoute, just get the record cache
return this.store.all('user');
},
setupController: function(controller, model) {
controller.set('model', model);
}
});
store.find - depends on how you call it, and depending on how you call it, it will do one of the following three things:
store.findAll - Always performs a server call. Also always returns the same RecordArray instance every time it is called.
store.findQuery - Always performs a server call. A different RecordArray (more specifically, an AdapterPopulatedRecordArray) will be returned on every call.
store.findById - Will only perform a server call if the record is not already loaded.
There is also store.filter, which will by default not perform a server call and only search for records that currently exist in the store. You can, however, tell filter to indeed perform a server call by supplying a query object, which will be delegated to a findQuery call under the hood.
OK, so it turns out my naive hack was indeed very naive. Turns out something like this works much more smoothly:
App.UserPhotosRoute = App.SecretRoute.extend
model: ->
user = @modelFor 'user'
if @controllerFor('user.photos').get('loadedForUser') == user
@controllerFor('user.photos').get('model')
else
@get('store').find('attachment', user_id: user.get('id'))
However, I do still have a number of other areas in the app that are affected by an inability to filter based on associations with async: true set. Is there a “right” or “ember” way to handle such cases?