Hi.
I am using Ember 1.13.5 and Ember Data 1.13.7.
In the blog post for the new Ember Data 1.13 it says:
Having observed many Ember apps in the wild, we have realized that neither of these two behaviors are the most common use case and deserving of being the default. The most commonly desired behavior we have seen in Ember apps is:
- First time store.find is called, fetch new data
- Next time return cached data
- Fetch new data in the background and update
This is the behavior of the new findRecord and findAll methods.
However, I am not getting this behaviour in my application and I’m wondering what I am doing wrong.
My router.js is as follows:
export default Router.map(function() {
this.route('elavonApplications', function() {
this.route('list');
});
this.route('elavonApplication', { path: '/elavonApplication/:id' });
});
The “elavon-applications.js” route file looks like this:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
// Always get related models.
elavonApplications: this.store.findAll('elavonApplication'),
isoApplicationStatuses: this.store.findAll('isoApplicationStatus')
});
}
});
The nested “list.js” route file looks like this:
import Ember from 'ember';
export default Ember.Route.extend({
queryParams: {
all: {
refreshModel: true
},
highRisk: {
refreshModel: true
},
prohibited: {
refreshModel: true
}
},
model: function(params) {
if(Ember.isPresent(params.highRisk)) {
return this.store.peekAll('elavonApplication').filterBy('highRisk', 1);
} else if(Ember.isPresent(params.prohibited)) {
return this.store.peekAll('elavonApplication').filterBy('prohibited', 1);
}
// No filter query parameters have been passed so return all application records from the store.
return this.store.peekAll('elavonApplication');
},
});
and the “elavon-application.js” route looks like this:
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return Ember.RSVP.hash({
elavonApplication: this.store.findRecord('elavonApplication', params.id),
isoApplicationStatuses: this.store.findAll('isoApplicationStatus'),
auditLog: this.store.query('log', {filter: {object: 'IsoApplication', object_id: params.id}}),
templates: this.store.findAll('template')
});
},
});
So, as I see it the “elavon-applications” route is hit first and pulls in all “elavonApplications” and “isoApplicationStatuses” from the server. The “list” route is then hit and grabs the already existing “elavonApplications” either filtered or not depending upon which query params have been passed in.
In application terms this produces a list of elavonApplications on screen in a table. If one of the table rows is clicked the “elavon-application” route is fired to display an individual elavonApplication on screen.
This all works fine. However, when I click the “back” button or click the on-screen link to transition back to the elavon-application list the data reloads from the server and the UI is in a “loading” state until the promises return. As we have already loaded all the elavon-applications and iso-application-statuses when we visited this route earlier and as we are using the new find methods why is the transition not occurring immediately? I would have expected no loading state and instead for the server requests to fire in the background with the UI displaying immediately.
Can anyone tell me what I am doing wrong or whether I am misunderstanding how the new “findAll”, “findRecord” etc methods work?
Thanks.