Sorting hasMany relationship after store.push()


#1

Hey everyone. I’ve scoured the web for the answer to this, but can’t figure it out. When I push new records into the store sorting is not applied in my controller.

Here is my basic models:

// models/service.js
export default DS.Model.extend({
  name: attr('string'),
  insertedAt: attr('date'),
  notifications: hasMany('notification', {async: true})
});

// models/notification.js
export default DS.Model.extend({
  service: DS.belongsTo('service'),
  body: DS.attr(),
});

I have a service which connects to a socket and and pushes new items to the store via store.push(). Here is what my controller looks like that does the sorting:

// controllers/service.js 
export default Ember.Controller.extend({
  notificationSorting: ['insertedAt:desc'],
  sortedNotifications: Ember.computed.sort('model.notifications', 'notificationSorting'),
});

When the view is first loaded and the notifications come from the API the sorting is correct. However whenever new items come in from the socket they are inserted at the end of the list in the template. The new notifications are supposed to pop up at the top of the view. Is there a way to accomplish this? It seems like the way it is should “just work”