Question

Lets say I have 2 nested resources for posts. It is defined like this in my router.

this.resource('posts', function () {
    this.resource('post', {path: '/:post_id'});
});

Each post can then also have multiple related comments.

In my *posts_route.js* i set up the model like this:

return this.get('store').findAll('post');

This makes a GET request to my backend api which returns all posts as json. At this point I only need basic information (and no comments), so not all the data is included in the returned json. Later, if I click on a specific post I do this in my *post_route.js*:

return this.get('store').find('post', params.post_id);

With this I wish to do a new GET request for only that specific post. But ember-data does not do a new server request. I looks for it in the local store where it finds it, but not with the extended data I was hoping to get.

Is there a way to force ember-data to do a real request to the server? Thanks.

Was it helpful?

Solution

You can use model.reload() to force a new GET request to the show route. You could do this in the afterModel hook or the setupController hook of your show route. If you really don't want to trigger this request again if the model has already been loaded, you can just define a plain old reloaded property on your model:

MyModel = DS.Model.extend({
   ... attributes ...
   reloaded: false
})

And then set that to true when the reload completes:

model.reload().then(function(response) { model.set('reloaded', true'); });

OTHER TIPS

A way to force ember-data to do a real request to the server would be like this:

store.findRecord('post', 1, { reload: true })

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top