6
votes

Is there a nice way to force Ember Data to load the resource from server eaven if it has it already in store ?

I have a simple show user action that do store.find('users',id) the model is loaded only once at first attempt to display a page the second time i go my model is loaded from the store which is normal ember data behaviour i know. However i need to load it each time.

edit: the only way i found is to do this :

@store.find('user',{id: params.user_id}).then (users)->
  users.get('firstObject')

however it forces me to implement a "fake" show action on my index action ...

6

6 Answers

3
votes

Additionally you can call getById which will return any instance of that record that exists, or null, then call unloadRecord to remove it from the cache. I like Edu's response as well though, then I wouldn't have to worry about the record existing somewhere else. Maybe I'd use getById then reload that way any references that had a reference to the user got updated. (pardon my errant coffeescript, if it's wrong).

user = @store.find('user', params.user_id)
if (user) 
  @store.unloadRecord(user)
2
votes

Hot off the presses, thanks to machty:

There's a new method getting added as part of the query params feature going into beta this weekend called Route.refresh()...

/**
Refresh the model on this route and any child routes, firing the
`beforeModel`, `model`, and `afterModel` hooks in a similar fashion
to how routes are entered when transitioning in from other route.
The current route params (e.g. `article_id`) will be passed in
to the respective model hooks, and if a different model is returned,
`setupController` and associated route hooks will re-fire as well.

An example usage of this method is re-querying the server for the
latest information using the same parameters as when the route
was first entered.

Note that this will cause `model` hooks to fire even on routes
that were provided a model object when the route was initially
entered.

@method refresh
@return {Transition} the transition object associated with this
  attempted transition
@since 1.4.0
*/
1
votes

You can do this in the setupController hook, using a promise, and the reload method mentioned by Edu.

  setupController: ->
    @store.find('myModel', 1).then (myModel) ->
      myModel.reload()
0
votes

If you are sure that records to display will change after a certain action then you can call this.refresh() method in your Route. For example:

ProductsRoute = Ember.Route.extend
  model: ->
    @store.find 'product',
      activated: true

  actions:
    accept: (product) ->
      if not product.get('activated')
        product.set 'activated', true
        product.save()
          .catch (err) ->
            console.log err
            product.rollback()
          .then =>
            @refresh()

    ignore: (product) ->
      if not product.get('ignored')
        product.set 'ignored', true
        product.save()
          .catch (err) ->
            console.log err
            product.rollback()
          .then =>
            @refresh()

If actions are called from child route - e.g. products/proposed - models will be reloaded for parent route and also child routes.

0
votes

I think that what you are looking for is DS.Store#fetchById