0

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.

Shahroon
  • 307
  • 2
  • 15

1 Answers1

1

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'),
uladzimir
  • 5,639
  • 6
  • 31
  • 50
Alex Berdyshev
  • 761
  • 2
  • 7
  • 21
  • My issue still exists, I did the following things, 1> created this new property "messageAtInt" in my cars model. 2> passed this new attribute to cars.sortBy('messageAtInt','name').reverse() Now the new message goes fine, at the top but rest of the messages does not sort accordingly. – Shahroon Jul 06 '15 at 17:09
  • First of all you should figure out whether `lastMessageTime.getTime()` works correctly and really converts your dateTime to milliseconds, the problem can be in different date format came from server and from your createRecord. The second issue can be your sorting function. For this case you can create a computed property which will sort you data, `sortProperties: ['lastMessageTimeInMilliseconds:desc'], sortedDialogs: Ember.computed.sort('model.content', 'sortProperties')` . You should do this inside of your controller of course – Alex Berdyshev Jul 06 '15 at 17:42
  • The first thing you mentioned about fomat, I think that's fine (coming like this 1436201554616), now as far as your second suggestion is concerned there is actually issue with that, I am having two different controllers cars and chats, the action you see above is in chats controller and the data to apply sorting on is in truck model. So how do you suggest that suggested sortProperties thing will work from one controller to another? – Shahroon Jul 07 '15 at 06:20
  • Format seems to be correct. To share some data between 2 controllers you can use needs - http://guides.emberjs.com/v1.12.0/controllers/dependencies-between-controllers/ . It is well described in the guides. – Alex Berdyshev Jul 07 '15 at 10:24