From 7e674852bbc884fcf43885b6dfb001cf22c3ecec Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sat, 11 Feb 2012 14:40:32 -0500 Subject: [PATCH] Clear completed & add todo at bottom of list --- .../backbone/models/habit.js.coffee | 17 ++++++++-- .../views/habits/index_view.js.coffee | 32 +++++++------------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/backbone/models/habit.js.coffee b/app/assets/javascripts/backbone/models/habit.js.coffee index 8607201e08..e2f7ed271d 100644 --- a/app/assets/javascripts/backbone/models/habit.js.coffee +++ b/app/assets/javascripts/backbone/models/habit.js.coffee @@ -9,7 +9,9 @@ class HabitTracker.Models.Habit extends Backbone.Model up: true down: true done: false - position: 0 + # TODO this is being calculated instead on habit creation in index_view + # because HabitsCollection instance isn't available here. Fix that + position: 0 isHabit: => @get("habit_type")==1 @@ -50,10 +52,21 @@ class HabitTracker.Models.Habit extends Backbone.Model done = false if direction=="down" @set({ score: score, done: done }) window.userStats.updateStats(this, delta) - + #send all the update information, as well as tack on userStats which will save to Users @save({ user_stats: window.userStats }) class HabitTracker.Collections.HabitsCollection extends Backbone.Collection model: HabitTracker.Models.Habit url: '/habits' + comparator: (habit) -> + habit.get("position") + + # Filter down the list of all todo items that are finished. + doneTodos: -> + @filter (todo) -> + todo.get('done') and todo.isTodo() + + nextPosition: -> + if (!@length) then return 1 + return @max() + 1 \ No newline at end of file diff --git a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee index 965b32337c..30980b2580 100644 --- a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee +++ b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee @@ -5,8 +5,8 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View events: "keypress .new-habit": "createOnEnter", - "keyup .new-habit": "showTooltip", - "click .todo-clear a": "clearCompleted" + # "keyup .new-habit": "showTooltip", #TODO get this function todo.js + "click .clear-completed": "clearCompleted" initialize: () -> @options.habits.bind('reset', @addAll) @@ -17,29 +17,19 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View createOnEnter: (e) -> input = $(e.target) if (!input.val() or e.keyCode != 13) then return - @options.habits.create {name: input.val(), habit_type: input.attr('data-type')} #, - #TODO - # success: (habit) => - # @model = habit - # error: (habit, jqXHR) => - # @model.set({errors: $.parseJSON(jqXHR.responseText)} + @options.habits.create {name: input.val(), habit_type: input.attr('data-type'), position: @options.habits.nextPosition()}, + #TODO what's this all about? + success: (habit) -> + @model = habit + error: (habit, jqXHR) -> + @model.set({errors: $.parseJSON(jqXHR.responseText)}) input.val('') - #TODO clearCompleted: -> - # _.each(Todos.done(), function(todo){ todo.destroy(); }); - # return false; + _.each @options.habits.doneTodos(), (todo) -> + todo.destroy() + return false - #TODO - showTooltip: (e) -> - # var tooltip = this.$(".ui-tooltip-top"); - # var val = this.input.val(); - # tooltip.fadeOut(); - # if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout); - # if (val == '' || val == this.input.attr('placeholder')) return; - # var show = function(){ tooltip.show().fadeIn(); }; - # this.tooltipTimeout = _.delay(show, 1000); - # TODO create a view & template, bind to existing element updateStats: () => stats = window.userStats