forked from tastejs/todomvc
/
main.js
185 lines (166 loc) · 4.73 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
define({
// Cujo uses OOCSS principles and thus separates theme (skin)
// from structure CSS.
theme: { module: 'css!theme/base.css' },
// The root node where all the views will be inserted
root: { $ref: 'dom!todoapp' },
// Render and insert the create view
createView: {
render: {
template: { module: 'text!create/template.html' },
replace: { module: 'i18n!create/strings' }
},
insert: { first: 'root' }
},
// Hook up the form to auto-reset whenever a new todo is added
createForm: {
element: { $ref: 'dom.first!form', at: 'createView' },
connect: { 'todos.onAdd': 'reset' }
},
// Render and insert the list of todos, linking it to the
// data and mapping data fields to the DOM
listView: {
render: {
template: { module: 'text!list/template.html' },
replace: { module: 'i18n!list/strings' },
css: { module: 'css!list/structure.css' }
},
insert: { after: 'createView' },
bind: {
to: { $ref: 'todos' },
comparator: 'dateCreated',
bindings: {
text: 'label, .edit',
complete: [
'.toggle',
{ attr: 'classList', handler: { module: 'list/setCompletedClass' } }
]
}
}
},
// Render and insert the "controls" view--this has the todo count,
// filters, and clear completed button.
controlsView: {
render: {
template: { module: 'text!controls/template.html' },
replace: { module: 'i18n!controls/strings' },
css: { module: 'css!controls/structure.css' }
},
insert: { after: 'listView' }
},
// Render and insert the footer. This is mainly static text, but
// is still fully internationalized.
footerView: {
render: {
template: { module: 'text!footer/template.html' },
replace: { module: 'i18n!footer/strings' }
},
insert: { after: 'root' }
},
// Create a localStorage adapter that will use the storage
// key 'todos-cujo' for storing todos. This is also linked,
// creating a two-way linkage between the listView and the
// data storage.
todoStore: {
create: {
module: 'cola/adapter/LocalStorage',
args: 'todos-cujo'
},
bind: {
to: { $ref: 'todos' }
}
},
todos: {
create: {
module: 'cola/Hub',
args: {
strategyOptions: {
validator: { module: 'create/validateTodo' }
}
}
},
before: {
add: 'cleanTodo | generateMetadata',
update: 'cleanTodo'
}
},
// The main controller, which is acting more like a mediator in this
// application by reacting to events in multiple views.
// Typically, cujo-based apps will have several (or many) smaller
// view controllers. Since this is a relatively simple application,
// a single controller fits well.
todoController: {
prototype: { create: 'controller' },
properties: {
todos: { $ref: 'todos' },
createTodo: { compose: 'parseForm | todos.add' },
removeTodo: { compose: 'todos.remove' },
updateTodo: { compose: 'todos.update' },
querySelector: { $ref: 'dom.first!' },
masterCheckbox: { $ref: 'dom.first!#toggle-all', at: 'listView' },
countNode: { $ref: 'dom.first!.count', at: 'controlsView' },
remainingNodes: { $ref: 'dom.all!#todo-count strong', at: 'controlsView' }
},
on: {
createView: {
'submit:form': 'createTodo'
},
listView: {
'click:.destroy': 'removeTodo',
'change:.toggle': 'updateTodo',
'click:#toggle-all': 'toggleAll',
'dblclick:label': 'todos.edit',
'change,focusout:.edit': 'todos.submit' // also need way to submit on [enter]
},
controlsView: {
'click:#clear-completed': 'removeCompleted'
}
},
connect: {
updateTotalCount: 'setTodosTotalState',
updateRemainingCount: 'setTodosRemainingState',
updateCompletedCount: 'setTodosCompletedState',
'todos.onChange': 'updateCount',
'todos.onEdit': 'todos.findNode | toggleEditingState.add | beginEditTodo',
'todos.onSubmit': 'todos.findNode | toggleEditingState.remove | todos.findItem | endEditTodo'
}
},
parseForm: { module: 'cola/dom/formToObject' },
cleanTodo: { module: 'create/cleanTodo' },
generateMetadata: { module: 'create/generateMetadata' },
toggleEditingState: {
create: {
module: 'wire/dom/transform/toggleClasses',
args: {
classes: 'editing'
}
}
},
setTodosTotalState: {
create: {
module: 'wire/dom/transform/cardinality',
args: { node: { $ref: 'root' }, prefix: 'todos' }
}
},
setTodosRemainingState: {
create: {
module: 'wire/dom/transform/cardinality',
args: { node: { $ref: 'root' }, prefix: 'remaining' }
}
},
setTodosCompletedState: {
create: {
module: 'wire/dom/transform/cardinality',
args: { node: { $ref: 'root' }, prefix: 'completed' }
}
},
plugins: [
// { module: 'wire/debug', trace: true },
{ module: 'wire/dom' },
{ module: 'wire/dom/render' },
{ module: 'wire/on' },
{ module: 'wire/aop' },
{ module: 'wire/connect' },
{ module: 'cola' }
]
});