@@ -178,7 +178,43 @@ TODOLIST = (function() {
178
178
} ) ;
179
179
} ,
180
180
181
- getMostImportantTask
181
+ saveTask : function ( task , callback ) {
182
+ db . transaction ( function ( transaction ) {
183
+ // if the task id is not assigned, then insert
184
+ if ( ! task . id ) {
185
+ transaction . executeSql (
186
+ "INSERT INTO task(name, description, due) VALUES (?, ?, ?);" ,
187
+ [ task . name , task . description , task . due ] ,
188
+ function ( tx ) {
189
+ transaction . executeSql (
190
+ "SELECT MAX(rowid) AS id from task" ,
191
+ [ ] ,
192
+ function ( tx , results ) {
193
+ task . id = results . rows . item ( 0 ) . id ;
194
+ if ( callback ) {
195
+ callback ( ) ;
196
+ } // if
197
+ }
198
+ ) ;
199
+ }
200
+ ) ;
201
+ }
202
+ // otherwise, update
203
+ else {
204
+ transaction . executeSql (
205
+ "UPDATE task " +
206
+ "SET name = ?, description = ?, due = ?, completed = ? " +
207
+ "WHERE rowid = ?;" ,
208
+ [ task . name , task . description , task . due , task . completed , task . id ] ,
209
+ function ( tx ) {
210
+ if ( callback ) {
211
+ callback ( ) ;
212
+ } // if
213
+ }
214
+ ) ;
215
+ } // if..else
216
+ } ) ;
217
+ }
182
218
} ;
183
219
184
220
return subModule ;
@@ -236,23 +272,23 @@ getMostImportantTask
236
272
/* view activation handlers */
237
273
238
274
activateMain : function ( ) {
239
- TODOLIST . Storage . getMostImportantTask ( function ( mit ) {
240
- if ( mit ) {
275
+ TODOLIST . Storage . getMostImportantTask ( function ( task ) {
276
+ if ( task ) {
241
277
// the no tasks message may be displayed, so remove it
242
278
jQuery ( "#main .notasks" ) . remove ( ) ;
243
279
244
280
// update the task details
245
- showTaskDetails ( "#main .task" , mit ) ;
281
+ showTaskDetails ( "#main .task" , task ) ;
246
282
247
283
// attach a click handler to the complete task button
248
284
jQuery ( "#main .task-complete" ) . unbind ( ) . click ( function ( ) {
249
285
jQuery ( "#main .task" ) . slideUp ( ) ;
250
286
251
287
// mark the task as complete
252
- mit . complete ( ) ;
288
+ task . complete ( ) ;
253
289
254
290
// save the task back to storage
255
- TODOLIST . Storage . saveTask ( mit , module . activateMain ) ;
291
+ TODOLIST . Storage . saveTask ( task , module . activateMain ) ;
256
292
} ) ;
257
293
}
258
294
else {
@@ -269,7 +305,7 @@ getMostImportantTask
269
305
270
306
populateTaskList ( ) ;
271
307
272
- // update the task list
308
+ // refresh the task list display
273
309
jQuery ( "ul#tasklist li" ) . click ( function ( ) {
274
310
toggleDetailsDisplay ( this ) ;
275
311
} ) ;
0 commit comments