Download data from ember server.

Is there a good way to get Ember Data to download a resource from an eaven server if it is already in the store?

I have a simple show user action that does store.find('users',id) , the model only loads once the first attempt to display the page the second time I load my model from the store, which is the normal behavior of ember data. which I know. However, I need to download it every time.

edit: the only way to find 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
source share
6 answers

I think this is ... http://emberjs.com/api/data/classes/DS.Model.html#method_reload

 model.reload() 

Good luck.

+5
source

Alternatively, you can call getById , which will return any instance of an existing record or null, and then call unloadRecord to remove it from the cache. I like Ed's reaction, though, then I don’t have to worry about the record, which is somewhere else. Perhaps I would use getById then reload so that all links containing the user link are updated. (have mercy on my weird coffeepot if he is mistaken).

 user = @store.find('user', params.user_id) if (user) @store.unloadRecord(user) 
+3
source

Hot clicks thanks to machty :

There, a new method, added as part of the request parameters function, which goes to beta in this weekend, is 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 (eg `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 */ 
+2
source

You can do this in the hookController using the promise and the reload method mentioned by Edu.

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

If you are sure that the displayed records will change after a certain action, you can call the this.refresh() method in your route. For instance:

 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 a child route - for example, products / proposed models will be reloaded for the parent route, as well as for children's routes.

0
source

I think you are looking for DS.Store # fetchById

0
source

Source: https://habr.com/ru/post/957828/


All Articles