Understanding ArrayController better - Sorting and counting items depending on date


#1

I have extended the Todo example with a time attribute to store the time of creation.

import DS from 'ember-data';
export default DS.Model.extend({
  title: DS.attr('string'),
  time: DS.attr('date', {
    defaultValue: function() { return new Date(); }
  })
});

In my little example I wanted to sort the entries by the time attribute, but it seems that sortAscending doesn’t have an effect.

export default Ember.ArrayController.extend({
  sortProperties: ['time'],
  sortAscending: false,
  ...

Is there any reason why sortAscending isn’t working? Is there maybe a conflict with the ObjectController?

In addition, I wanted to count all Todo items created today. For that I tried to create the method below, but it seems that ArrayController doesn’t have the method gte. How can I count the items for today without the gte method?

  // app/controllers/todos.js
  ...
  countTodos: function() {
    // count all todos added since midnight
    var d = new Date();
    d.setHours(0,0,0,0);
    return this.gte('time', d).get('length'); // gte isn't available here
  }.property('@each.time'),
  ...

#2
  1. You’ll have to use the controller’s property arrangedContent in order to display an ordered list.
  2. Use the Enumerable/Array’s filter method. Here’s an example.