SortBy in Ember.js not working on newly created Items

300 views Asked by At

I applied a sortBy on date(time) attribute like this,

cars = @store.all('car')
cars.filter (car) =>
   if car.get('name') == undefined
      @store.find('car', car.get('id')).then ->
      console.log('record found')
   else
      cars

cars.sortBy('messageAt','name').reverse()

It does work when I reload the page, but as soon as I get a new message in Ember data, it does appear in messages but at the bottom, which means sorting does not work.

1

There are 1 answers

4
Alex Berdyshev On

I got the same error. As far as I understood the problem is in messageAt attribute which has date type. And sorting by date is a little buggy in new versions of emberData. I fixed it by adding new property with integer type which is being sorted perfectly. Here's the code

lastMessageTime: DS.attr('date'),
lastMessageTimeInMilliseconds: function () {
    var lastMessageTime = this.get('lastMessageTime');

    if (typeof lastMessageTime === 'string') {
      return (new Date(lastMessageTime)).getTime();
    } else {
      return lastMessageTime.getTime();
    }

}.property('lastMessageTime'),