forked from wannesm/todos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (66 loc) · 1.92 KB
/
app.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Todos = SC.Application.create();
// SUGGESTION: Your tags can be stored in a similar way as how todos are stored
Todos.Todo = SC.Object.extend({
title: null,
isDone: false
});
Todos.Label = SC.Object.extend({
title: null,
isRelevant: false
});
Todos.labelsController = SC.ArrayProxy.create({
content: [],
recommendLabelsFor: function(todo){
var label = Todos.Label.create({ title: "label 1" });
this.pushObject(label);
},
clearLabels: function(){
this.set('content', []);
}
});
Todos.todosController = SC.ArrayProxy.create({
content: [],
createTodo: function(title) {
var todo = Todos.Todo.create({ title: title });
this.pushObject(todo);
},
clearCompletedTodos: function() {
this.filterProperty('isDone', true).forEach(this.removeObject, this);
},
remaining: function() {
return this.filterProperty('isDone', false).get('length');
}.property('@each.isDone'),
allAreDone: function(key, value) {
if (value !== undefined) {
this.setEach('isDone', value);
return value;
} else {
return !!this.get('length') && this.everyProperty('isDone', true);
}
}.property('@each.isDone')
});
Todos.StatsView = SC.View.extend({
remainingBinding: 'Todos.todosController.remaining',
remainingString: function() {
var remaining = this.get('remaining');
return remaining + (remaining === 1 ? " item" : " items");
}.property('remaining')
});
// SUGGESTION: Think about using SC.TextArea to get a bigger input field
Todos.CreateTodoView = SC.TextField.extend({
insertNewline: function() {
var value = this.get('value');
if (value) {
Todos.todosController.createTodo(value);
this.set('value', '');
Todos.labelsController.clearLabels();
}
},
keyDown: function(evt) {
var spaceKey = 32;
var value = this.get('value');
if (evt.keyCode === spaceKey) {
Todos.labelsController.recommendLabelsFor(value);
}
}
});