From ff21850e86959c1a2fe023804b845dd811ce41c5 Mon Sep 17 00:00:00 2001 From: meth Date: Fri, 28 Sep 2012 19:41:58 +0200 Subject: [PATCH] Can mark tasks as done and reopen them --- index.html | 5 ++++- js/main.js | 1 - js/models/tasker.js | 14 ++++++++++++++ js/script.js | 7 +++++-- js/views/list.js | 23 ++++++++++++++++++++--- js/views/task_list.js | 20 ++++++++++++++++++-- 6 files changed, 61 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 9580aec..9e20976 100644 --- a/index.html +++ b/index.html @@ -116,7 +116,10 @@

Assigned:

+ diff --git a/js/main.js b/js/main.js index 40572ff..645cbbd 100644 --- a/js/main.js +++ b/js/main.js @@ -4,7 +4,6 @@ requirejs.config({ 'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min', 'bootstrap': 'libs/bootstrap.min', 'backbone-lib': 'libs/backbone-min', - //'backbone-lib': 'http://backbonejs.org/backbone', 'backbone-debug': 'libs/backbone.debug', 'backbone': 'libs/backbone.localStorage-min', 'underscore': 'libs/underscore-min', diff --git a/js/models/tasker.js b/js/models/tasker.js index 75afb32..5841b64 100644 --- a/js/models/tasker.js +++ b/js/models/tasker.js @@ -4,6 +4,20 @@ define(['backbone', 'collections/task'], function( Backbone, TaskCollection ) defaults: { 'tasks': new TaskCollection(), 'done': new TaskCollection() + }, + markAsDone: function( id ) + { + var model = this.get( 'tasks' ).get( id ); + + this.get( 'tasks' ).remove( id ); + this.get( 'done' ).add( model ); + }, + reopenTask: function( id ) + { + var model = this.get( 'done' ).get( id ); + + this.get( 'done' ).remove( id ); + this.get( 'tasks' ).add( model ); } }); diff --git a/js/script.js b/js/script.js index 12ad77c..bbc308f 100644 --- a/js/script.js +++ b/js/script.js @@ -3,7 +3,7 @@ define(['jquery', 'backbone', 'models/tasker', 'views/list', 'views/form', 'view var AppRouter = Backbone.Router.extend( { routes: { 'add': 'add', - 'view-:id': 'view', + 'view/:id': 'view', 'edit-:id': 'add', '*home': 'home' }, @@ -13,13 +13,16 @@ define(['jquery', 'backbone', 'models/tasker', 'views/list', 'views/form', 'view { this.tasks = new Tasker(); this.views.listtodo = new ListView( {collection: this.tasks.get( 'tasks' )} ); - this.views.listdone = new ListView( {collection: this.tasks.get( 'done' ), el: document.getElementById( 'tasklist-done' )} ); + this.views.listdone = new ListView( {collection: this.tasks.get( 'done' ), el: document.getElementById( 'tasklist-done' ), template: _.template( document.getElementById( 'tasktemplate-done' ).innerHTML ) } ); this.views.task = new TaskView(); this.views.form = new FormView( {collection: this.tasks.get( 'tasks' )} ); this.tasks.get( 'tasks' ).on( 'add remove change', this.goToHome, this ); this.tasks.get( 'tasks' ).fetch(); + + this.views.listtodo.on( 'task:markAsDone', this.tasks.markAsDone, this.tasks ); + this.views.listdone.on( 'task:reopen', this.tasks.reopenTask, this.tasks ); }, showOnly: function ( section ) { diff --git a/js/views/list.js b/js/views/list.js index 176b4f6..506f9a7 100644 --- a/js/views/list.js +++ b/js/views/list.js @@ -2,18 +2,27 @@ define(['jquery', 'underscore', 'backbone', 'views/task_list', 'views/tabs'], fu { var ListView = Backbone.View.extend( { el: document.getElementById( 'tasklist' ), - template: _.template( document.getElementById( 'tasktemplate' ).innerHTML ), + template: null, tabs: new TabsView(), - initialize: function () + initialize: function ( options ) { + if ( options.template ) + { + this.template = options.template; + } + this.collection.on( 'add', this.addOne, this ); this.collection.on( 'reset', this.render, this ); }, addOne: function ( model ) { - var view = new SingleTaskListView( {model: model} ); + var view = new SingleTaskListView( {model: model, template: this.template} ); view.render(); + + view.on( 'task:markAsDone', this.markAsDone, this ); + view.on( 'task:reopen', this.reopenTask, this ); + this.$el.append( view.el ); }, render: function ( collection ) @@ -26,6 +35,14 @@ define(['jquery', 'underscore', 'backbone', 'views/task_list', 'views/tabs'], fu showTab: function( tab ) { this.tabs.showTab( tab ); + }, + markAsDone: function( model ) + { + this.trigger( 'task:markAsDone', model.get( 'id' ) ); + }, + reopenTask: function( model ) + { + this.trigger( 'task:reopen', model.get( 'id' ) ); } } ); diff --git a/js/views/task_list.js b/js/views/task_list.js index 730416b..64a939f 100644 --- a/js/views/task_list.js +++ b/js/views/task_list.js @@ -4,10 +4,16 @@ define(['jquery', 'underscore', 'backbone'], function( $, _, Backbone ) template: _.template( document.getElementById( 'tasktemplate' ).innerHTML ), tagName: 'li', events: { - 'click a[href^="#remove"]': 'removeModel' + 'click a[href^="#remove"]': 'removeModel', + 'click a[href="#done"]': 'markAsDone', + 'click a[href="#reopen"]': 'reopenTask' }, - initialize: function() + initialize: function( options ) { + if ( options.template ) + { + this.template = options.template; + } this.model.on( 'destroy remove', this.remove, this ); this.model.on( 'change', this.render, this ); }, @@ -27,6 +33,16 @@ define(['jquery', 'underscore', 'backbone'], function( $, _, Backbone ) options.id = this.model.cid this.el.innerHTML = this.template( options ); return this; + }, + markAsDone: function( e ) + { + this.trigger( 'task:markAsDone', this.model ); + e.preventDefault(); + }, + reopenTask: function( e ) + { + this.trigger( 'task:reopen', this.model ); + e.preventDefault(); } });