2

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.

Oscar
  • 383
  • 3
  • 13
  • What if you first set the post item dirty? See: http://stackoverflow.com/questions/16158329/ember-data-how-to-set-isdirty-for-a-record – DelphiLynx Jan 09 '14 at 09:03
  • @DelphiLynx Thanks for your response. I like your idea, but I can't get it to set the post to dirty. Should try making it dirty in my post_route just before I do find('post', params.post_id)? – Oscar Jan 09 '14 at 11:59
  • @DelphiLynx I ended up solving my problem finally. The problem was actually a bit different from how I presented it. I will edit my question and write how I solved it tomorrow. Thanks again. – Oscar Jan 09 '14 at 22:34
  • @@Oscar, what I also did in an application was to have a custom Dirty boolean property in my object. That way I could set the whole model dirty to just toggle that property. Just an idea :) – DelphiLynx Jan 10 '14 at 07:38
  • @DelphiLynx I came to work to day feeling all good having fixed the problem. Then I found out that the external api that the backend is using would not support it. So I'm going to go for toggling a dirty boolean like you suggested. Seems like the way to go right now. :) – Oscar Jan 10 '14 at 08:52

2 Answers2

1

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'); });
Nick Ragaz
  • 1,352
  • 2
  • 11
  • 18
1

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

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

Ashikodi
  • 1,090
  • 1
  • 9
  • 9