summaryrefslogtreecommitdiff
path: root/js/controllers/todos_controller.js
blob: f7f2434c7ce129a4632582faae157562059b0862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Todos.TodosController = Ember.ArrayController.extend({
  createTodo: function(){
    var title = this.get('newTitle');
    if (!title.trim()) { return; }

    var todo = Todos.Todo.createRecord({
      title: title,
      isCompleted: false
    });

    this.set('newTitle', '');
    todo.save();
  },
  remaining: function(){
    return this.filterProperty('isCompleted', false).get('length');
  }.property('@each.isCompleted'),
  inflection: function() {
    var remaining = this.get('remaining');
    return remaining === 1 ? 'item' : 'items';
  }.property('remaining'),
  hasCompleted: function(){
    return this.get('completed') > 0;
  }.property('completed'),
  completed: function(){
    return this.filterProperty('isCompleted', true).get('length');
  }.property('@each.isCompleted'),
  clearCompleted: function() {
    var completed = this.filterProperty('isCompleted', true);
    completed.invoke('deleteRecord');
    this.get('store').commit();
  },
  allAreDone: function(key, value) {
    if (value === undefined) {
      return !!this.get('length') && this.everyProperty('isCompleted', true);
    } else {
      this.setEach('isCompleted', value);
      this.get('store').save();
      return value;
    }
  }.property('@each.isCompleted')
});