Ember-data store not reread


#1

I have the following code:

  model() {
		return this.store.findAll('category').then((categories) => {
			return categories.sortBy('name');
		});
	},

Data is read and sorted the first time the route is accessed but not when I add or delete rows. These changes are not shown on the template until I do a reload of the page even I can see them in the store. If I just do the simple

return this.store.findAll('category')

changes are shown immediately but of course not sorted.

Any clues to this problem?


#2

Instead of sorting the model in the route, try sorting is as a computed property in your controller or component.

E.g.

categories: Ember.computed('model', function() {
  return this.get('model').sortBy('name');
}

#3

Thank’s I’ll try this. I just found it strange that it only works once in the router.


#4

I have now tried to put the sorting code in a controller but the result is the same, the list does not update when a category is added only after a restore of the app.

Just to clarify: The category is added to the store and the backend database, so I need the template to be refreshed with the new contents of the store and sorted by the specified criteria.


#5

The computed property specified above has incorrect dependent keys. Have a look at the guide to see how to make computed properties that work with arrays: https://guides.emberjs.com/v2.11.0/object-model/computed-properties-and-aggregate-data/

Although for this use case, I suggest using Ember.computed.sort instead, because it’s a pre-made solution for your problem


#6

Thank you, I’ll try it