|
1 | 1 | //a custom binding to handle the enter key
|
2 | 2 | o_O.bindings.enterKey = function( func, $el ) {
|
3 |
| - var ENTER_KEY = 13; |
4 |
| - var context = this; |
5 |
| - $el.keyup(function(e) { |
6 |
| - if( e.keyCode === ENTER_KEY ) |
7 |
| - func.call(context); |
8 |
| - }) |
| 3 | + var ENTER_KEY = 13; |
| 4 | + var context = this; |
| 5 | + $el.keyup(function(e) { |
| 6 | + if( e.keyCode === ENTER_KEY ) |
| 7 | + func.call(context); |
| 8 | + }) |
9 | 9 | }
|
10 | 10 | o_O.bindingTypes.enterKey = 'outbound'
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | // represents a single todo item
|
14 | 14 | var Todo = o_O.model.extend({
|
15 |
| - title: '', |
16 |
| - completed: false |
17 |
| - }, |
18 |
| - { |
19 |
| - initialize: function() { |
20 |
| - this.editing = o_O(false) |
21 |
| - }, |
22 |
| - |
23 |
| - startEditing: function() { |
24 |
| - this.editing( true ) |
25 |
| - var self = this |
26 |
| - setTimeout(function() { |
27 |
| - $(self.el).parent().find('input.edit').focus().select() |
28 |
| - }, 0) |
29 |
| - }, |
30 |
| - |
31 |
| - stopEditing: function() { |
32 |
| - var text = $.trim( this.title() ) |
33 |
| - |
34 |
| - text |
35 |
| - ? this.title( text ) |
36 |
| - : this.remove() |
37 |
| - |
38 |
| - this.editing( false ) |
39 |
| - }, |
40 |
| - |
41 |
| - remove: function() { |
42 |
| - todoapp.todos.remove( this ) |
43 |
| - }, |
44 |
| - |
45 |
| - visible: function() { |
46 |
| - var filter = todoapp.filter(), |
47 |
| - completed = this.completed() |
48 |
| - return filter == '' || (filter == 'completed' && completed) || (filter == 'active' && !completed) |
49 |
| - }, |
50 |
| - |
51 |
| - klass: function() { |
52 |
| - if(this.editing()) |
53 |
| - return 'editing' |
54 |
| - if(this.completed()) |
55 |
| - return 'completed' |
56 |
| - else |
57 |
| - return '' |
58 |
| - } |
59 |
| - } |
| 15 | + title: '', |
| 16 | + completed: false |
| 17 | + }, |
| 18 | + { |
| 19 | + initialize: function() { |
| 20 | + this.editing = o_O(false) |
| 21 | + }, |
| 22 | + |
| 23 | + startEditing: function() { |
| 24 | + this.editing( true ) |
| 25 | + var self = this |
| 26 | + setTimeout(function() { |
| 27 | + $(self.el).parent().find('input.edit').focus().select() |
| 28 | + }, 0) |
| 29 | + }, |
| 30 | + |
| 31 | + stopEditing: function() { |
| 32 | + var text = $.trim( this.title() ) |
| 33 | + |
| 34 | + text |
| 35 | + ? this.title( text ) |
| 36 | + : this.remove() |
| 37 | + |
| 38 | + this.editing( false ) |
| 39 | + }, |
| 40 | + |
| 41 | + remove: function() { |
| 42 | + todoapp.todos.remove( this ) |
| 43 | + }, |
| 44 | + |
| 45 | + visible: function() { |
| 46 | + var filter = todoapp.filter(), |
| 47 | + completed = this.completed() |
| 48 | + return filter == '' || (filter == 'completed' && completed) || (filter == 'active' && !completed) |
| 49 | + }, |
| 50 | + |
| 51 | + klass: function() { |
| 52 | + if(this.editing()) |
| 53 | + return 'editing' |
| 54 | + if(this.completed()) |
| 55 | + return 'completed' |
| 56 | + else |
| 57 | + return '' |
| 58 | + } |
| 59 | + } |
60 | 60 | );
|
61 | 61 |
|
62 | 62 | // main application
|
63 | 63 | var TodoApp = o_O.model.extend({
|
64 |
| - current: '', |
65 |
| - completedCount: 0, |
66 |
| - filter: '' |
67 |
| - }, { |
68 |
| - initialize: function() { |
69 |
| - var self = this |
70 |
| - self.todos = o_O.array( this.todos() ) |
71 |
| - |
72 |
| - this.todos.on('set:completed set:title add remove', function() { |
73 |
| - var completed = self.todos.filter(function(todo) { |
74 |
| - return todo.completed() |
75 |
| - }) |
76 |
| - self.completedCount( completed.length ) |
77 |
| - self.persist() |
78 |
| - }) |
79 |
| - |
80 |
| - this.remainingCount = o_O(function() { |
81 |
| - return self.todos.count() - self.completedCount() |
82 |
| - }) |
83 |
| - |
84 |
| - // writeable computed observable |
85 |
| - // handles marking all complete/incomplete |
86 |
| - // or retrieving if this is true |
87 |
| - this.allCompleted = o_O(function(v) { |
88 |
| - if(arguments.length == 0) { |
89 |
| - return self.remainingCount() == 0 |
90 |
| - } |
91 |
| - |
92 |
| - self.todos.each(function(todo) { |
93 |
| - todo.completed( v ) |
94 |
| - }) |
95 |
| - |
96 |
| - return v |
97 |
| - }) |
98 |
| - |
99 |
| - }, |
100 |
| - |
101 |
| - add: function() { |
102 |
| - var text = $.trim( this.current() ); |
103 |
| - if( text ) { |
104 |
| - this.todos.unshift( Todo({title: text}) ); |
105 |
| - this.current( "" ) |
106 |
| - } |
107 |
| - }, |
108 |
| - |
109 |
| - removeCompleted: function () { |
110 |
| - this.todos.remove( function(todo) { |
111 |
| - return todo.completed() |
112 |
| - }) |
113 |
| - return false |
114 |
| - }, |
115 |
| - |
116 |
| - persist: function() { |
117 |
| - localStorage[ 'todos-o_O' ] = JSON.stringify( this.todos.toJSON() ) |
118 |
| - }, |
119 |
| - |
120 |
| - // adds an `s` where necessary |
121 |
| - pluralize: function( word, count ) { |
122 |
| - return word + (count === 1 ? "" : "s"); |
123 |
| - } |
124 |
| - } |
| 64 | + current: '', |
| 65 | + completedCount: 0, |
| 66 | + filter: '' |
| 67 | + }, { |
| 68 | + initialize: function() { |
| 69 | + var self = this |
| 70 | + self.todos = o_O.array( this.todos() ) |
| 71 | + |
| 72 | + this.todos.on('set:completed set:title add remove', function() { |
| 73 | + var completed = self.todos.filter(function(todo) { |
| 74 | + return todo.completed() |
| 75 | + }) |
| 76 | + self.completedCount( completed.length ) |
| 77 | + self.persist() |
| 78 | + }) |
| 79 | + |
| 80 | + this.remainingCount = o_O(function() { |
| 81 | + return self.todos.count() - self.completedCount() |
| 82 | + }) |
| 83 | + |
| 84 | + // writeable computed observable |
| 85 | + // handles marking all complete/incomplete |
| 86 | + // or retrieving if this is true |
| 87 | + this.allCompleted = o_O(function(v) { |
| 88 | + if(arguments.length == 0) { |
| 89 | + return self.remainingCount() == 0 |
| 90 | + } |
| 91 | + |
| 92 | + self.todos.each(function(todo) { |
| 93 | + todo.completed( v ) |
| 94 | + }) |
| 95 | + |
| 96 | + return v |
| 97 | + }) |
| 98 | + |
| 99 | + }, |
| 100 | + |
| 101 | + add: function() { |
| 102 | + var text = $.trim( this.current() ); |
| 103 | + if( text ) { |
| 104 | + this.todos.unshift( Todo({title: text}) ); |
| 105 | + this.current( "" ) |
| 106 | + } |
| 107 | + }, |
| 108 | + |
| 109 | + removeCompleted: function () { |
| 110 | + this.todos.remove( function(todo) { |
| 111 | + return todo.completed() |
| 112 | + }) |
| 113 | + return false |
| 114 | + }, |
| 115 | + |
| 116 | + persist: function() { |
| 117 | + localStorage[ 'todos-o_O' ] = JSON.stringify( this.todos.toJSON() ) |
| 118 | + }, |
| 119 | + |
| 120 | + // adds an `s` where necessary |
| 121 | + pluralize: function( word, count ) { |
| 122 | + return word + (count === 1 ? "" : "s"); |
| 123 | + } |
| 124 | + } |
125 | 125 | );
|
126 | 126 |
|
127 | 127 | function main() {
|
128 |
| - // load todos |
129 |
| - var todos = [] |
130 |
| - try { |
131 |
| - todos = JSON.parse( localStorage['todos-o_O'] ); |
132 |
| - } |
133 |
| - catch(e) { } |
134 |
| - |
135 |
| - // create models |
136 |
| - for( var i=0; i < todos.length; i++ ) |
137 |
| - todos[ i ] = Todo.create( todos[i] ) |
138 |
| - |
139 |
| - // create app |
140 |
| - window.todoapp = TodoApp( {todos: todos} ) |
141 |
| - |
142 |
| - // bind to DOM element |
143 |
| - todoapp.bind('#todoapp') |
144 |
| - |
145 |
| - |
146 |
| - // setup Routing |
147 |
| - o_O.router() |
148 |
| - .add('*filter', function(filt) { |
149 |
| - todoapp.filter(filt) |
150 |
| - |
151 |
| - $( '#filters a' ) |
152 |
| - .removeClass( 'selected' ) |
153 |
| - .filter( "[href='#/" + filt + "']" ) |
154 |
| - .addClass( 'selected' ) |
155 |
| - }) |
156 |
| - .start() |
| 128 | + // load todos |
| 129 | + var todos = [] |
| 130 | + try { |
| 131 | + todos = JSON.parse( localStorage['todos-o_O'] ); |
| 132 | + } |
| 133 | + catch(e) { } |
| 134 | + |
| 135 | + // create models |
| 136 | + for( var i=0; i < todos.length; i++ ) |
| 137 | + todos[ i ] = Todo.create( todos[i] ) |
| 138 | + |
| 139 | + // create app |
| 140 | + window.todoapp = TodoApp( {todos: todos} ) |
| 141 | + |
| 142 | + // bind to DOM element |
| 143 | + todoapp.bind('#todoapp') |
| 144 | + |
| 145 | + |
| 146 | + // setup Routing |
| 147 | + o_O.router() |
| 148 | + .add('*filter', function(filt) { |
| 149 | + todoapp.filter(filt) |
| 150 | + |
| 151 | + $( '#filters a' ) |
| 152 | + .removeClass( 'selected' ) |
| 153 | + .filter( "[href='#/" + filt + "']" ) |
| 154 | + .addClass( 'selected' ) |
| 155 | + }) |
| 156 | + .start() |
157 | 157 | }
|
158 | 158 |
|
159 | 159 | // kick it off
|
|
0 commit comments