Backbone basically working for habits. still no user data or rewards

This commit is contained in:
Tyler Renelle 2012-02-08 20:40:43 -05:00
parent 34c1d5d46f
commit 58eca5a413
13 changed files with 189 additions and 86 deletions

View file

@ -3,14 +3,67 @@ class HabitTracker.Models.Habit extends Backbone.Model
defaults:
name: null
habit_type: null
score: null
habit_type: 1
score: 0.0
notes: null
up: null
down: null
done: null
position: null
up: true
down: true
done: false
position: 0
isHabit: ->
@get("habit_type")==1
isDaily: ->
@get("habit_type")==2
isDoneTodo: ->
@get("habit_type")==3 and @get("done")
isRemainingTodo: ->
@get("habit_type")==3 and !@get("done")
# TODO before_save on user, calculate delta on score & experience there, not in vote
# TODO retrieve this formula from server so don't have to have it in two locations (Habit.clear requires it server-side)
vote: (direction) ->
# For negative values, use a line: something like y=-.1x+1
# For positibe values, taper off with inverse log: y=.9^x
# Would love to use inverse log for the whole thing, but after 13 fails it hits infinity
sign = if (direction == "up") then 1 else -1
score = this.get("score")
delta = 0
if score < 0
delta = (( -0.1 * score + 1 ) * sign)
else
delta = (( Math.pow(0.9, score) ) * sign)
score += delta
# up/down -voting as checkbox & assigning as done, 2 birds one stone
done = this.get("done")
if this.get("habit_type")!=1
done = true if direction=="up"
done = false if direction=="down"
this.save({ score: score, done: done })
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
url: '/habits'
#TODO
# standard: () ->
# @filter (habit)->
# habit.get('habit_type')==1
#
# daily: () ->
# @filter (habit) ->
# habit.get('habit_type')==2
#
# todosDone: () ->
# @filter (habit) ->
# (habit.get "done") and (habit.get('habit_type')==3)
#
# todosRemaining: () ->
# @without.apply(this, this.done()) and (habit.get('habit_type')==3)

View file

@ -11,11 +11,6 @@
<input type="text" name="habit_type" id="habit_type" value="<%= habit_type %>" >
</div>
<div class="field">
<label for="score"> score:</label>
<input type="text" name="score" id="score" value="<%= score %>" >
</div>
<div class="field">
<label for="notes"> notes:</label>
<input type="text" name="notes" id="notes" value="<%= notes %>" >
@ -31,16 +26,6 @@
<input type="text" name="down" id="down" value="<%= down %>" >
</div>
<div class="field">
<label for="done"> done:</label>
<input type="text" name="done" id="done" value="<%= done %>" >
</div>
<div class="field">
<label for="position"> position:</label>
<input type="text" name="position" id="position" value="<%= position %>" >
</div>
<div class="actions">
<input type="submit" value="Update Habit" />
</div>

View file

@ -1,12 +1,27 @@
<td><%= name %></td>
<td><%= habit_type %></td>
<td><%= score %></td>
<td><%= notes %></td>
<td><%= up %></td>
<td><%= down %></td>
<td><%= done %></td>
<td><%= position %></td>
<td><a href="#/<%= id %>">Show</td>
<td><a href="#/<%= id %>/edit">Edit</td>
<td><a href="#/<%= id %>/destroy" class="destroy">Destroy</a></td>
<li id="habit_<%= id %>" class="habit habit-type-<%= habit_type %> <!-- TODO score_color -->">
<!-- Habits -->
<% if (habit_type==1) { %>
<a href="#" class="vote-up">+</a>
<a href="#" class="vote-down">-</a>
<%= name %>
<% } else { %>
<!-- Daily & Todos -->
<% if (done) { %>
<a href="#" class="vote-down">[X]</a>
<strike><%= name %></strike>
<% } else { %>
<a href="#" class="vote-up">[ ]</a>
<%= name %>
<% } %>
<% } %>
<div class='edit'>
<% if (notes) { %>
<!-- TODO image_tag('comment.png', :class=>'comment') -->
<img src='/images/comment.png' class="comment" />
<div class='qtip'><%= notes %></div>
<% } %>
<a href="#/<%= id %>/edit">Edit</a>
</div>
</li>

View file

@ -1,21 +1,75 @@
<h1>Listing habits</h1>
<a href="#/new">New Habit</a>
<table id="habits-table">
<!--<ul id="habits-table" />-->
<table id="layout">
<tr>
<th>Name</th>
<th>Habit type</th>
<th>Score</th>
<th>Notes</th>
<th>Up</th>
<th>Down</th>
<th>Done</th>
<th>Position</th>
<th></th>
<th></th>
<th></th>
<td class="panel">
<div class="block">
<div class='content'>
<h2 class='title'>Habits</h2>
<div class='inner'>
<ul id="habits-habits" />
</div>
</div>
</div>
</td>
<td class="panel">
<div class="block">
<div class='content'>
<h2 class='title'>Daily</h2>
<div class='inner'>
<ul id="habits-daily" />
</div>
</div>
</div>
</td>
<td class="panel">
<div class="block">
<div class='content'>
<h2 class='title'>Todos</h2>
<div class='inner'>
<div id="habits-todos">
<ul>
<li><a href="#tabs-1">Remaining</a></li>
<li><a href="#tabs-2">Done</a></li>
</ul>
<div id="tabs-1">
<ul id="habits-todos-remaining" />
</div>
<div id="tabs-2">
<ul id="habits-todos-done" />
</div>
</div>
</div>
</div>
</div>
<!-- TODO link_to 'View Completed', :action=>'completed' -->
</td>
<td class="panel">
<div class="block">
<div class='content'>
<h2 class='title'>Rewards
<!-- TODO render 'shared/money' -->
</h2>
<div class='inner'>
<!-- TODO rewards code block (see old code) -->
<ul id="rewards" />
</div>
</div>
</div>
<!-- TODO link_to 'New Reward', new_reward_path -->
</td>
</tr>
</table>
<br/>
<a href="#/new">New Habit</a>
<br/>

View file

@ -11,11 +11,6 @@
<input type="text" name="habit_type" id="habit_type" value="<%= habit_type %>" >
</div>
<div class="field">
<label for="score"> score:</label>
<input type="text" name="score" id="score" value="<%= score %>" >
</div>
<div class="field">
<label for="notes"> notes:</label>
<input type="text" name="notes" id="notes" value="<%= notes %>" >
@ -31,16 +26,6 @@
<input type="text" name="down" id="down" value="<%= down %>" >
</div>
<div class="field">
<label for="done"> done:</label>
<input type="text" name="done" id="done" value="<%= done %>" >
</div>
<div class="field">
<label for="position"> position:</label>
<input type="text" name="position" id="position" value="<%= position %>" >
</div>
<div class="actions">
<input type="submit" value="Create Habit" />
</div>

View file

@ -13,7 +13,7 @@ class HabitTracker.Views.Habits.EditView extends Backbone.View
@model.save(null,
success : (habit) =>
@model = habit
window.location.hash = "/#{@model.id}"
window.location.hash = "#/index"
)
render : ->

View file

@ -5,15 +5,31 @@ class HabitTracker.Views.Habits.HabitView extends Backbone.View
events:
"click .destroy" : "destroy"
"click .vote-up" : "voteUp"
"click .vote-down" : "voteDown"
tagName: "tr"
tagName: "li"
destroy: () ->
@model.destroy()
this.remove()
return false
voteUp: ->
@model.vote("up")
@trigger("reset")
# console.log($(@el).parent().sibling('#habits-todos-done'))
# if @model.isRemainingTodo and $(@el).parent().id=="habits-remaining-todos"
# $(@el).parent().sibling('#habits-done-todos').append(this)
voteDown: ->
@model.vote("down")
render: ->
$(@el).html(@template(@model.toJSON() ))
@$(".comment").qtip content:
text: (api) ->
$(this).next().html()
return this

View file

@ -5,16 +5,21 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View
initialize: () ->
@options.habits.bind('reset', @addAll)
#TODO this is causing "Remaining" tab to be auto-selected
@options.habits.bind('change', @render, this)
addAll: () =>
@options.habits.each(@addOne)
addOne: (habit) =>
view = new HabitTracker.Views.Habits.HabitView({model : habit})
@$("tbody").append(view.render().el)
if habit.isHabit() then @$("#habits-habits").append(view.render().el)
if habit.isDaily() then @$("#habits-daily").append(view.render().el)
if habit.isDoneTodo() then @$("#habits-todos-done").append(view.render().el)
if habit.isRemainingTodo() then @$("#habits-todos-remaining").append(view.render().el)
render: =>
$(@el).html(@template(habits: @options.habits.toJSON() ))
@addAll()
return this
@$("#habits-todos").tabs()
return this

View file

@ -23,7 +23,7 @@ class HabitTracker.Views.Habits.NewView extends Backbone.View
@collection.create(@model.toJSON(),
success: (habit) =>
@model = habit
window.location.hash = "/#{@model.id}"
window.location.hash = "#/index"
error: (habit, jqXHR) =>
@model.set({errors: $.parseJSON(jqXHR.responseText)})

View file

@ -7,10 +7,6 @@ $(document).ready ->
$(".one-time a.vote-link").click ->
$(this).parent().fadeOut()
$(".comment").qtip content:
text: (api) ->
$(this).next().html()
$.each ['#habits', '#daily', '#one-offs'], (index, list_id) ->
$(list_id).sortable
axis: "y"

View file

@ -36,12 +36,10 @@
cursor: move;
}
.up, .down, .check {
.vote-up, .vote-down {
text-decoration:none;
}
.up, .down { font-weight: bold; }
.edit { float:right; }
#progressbar { margin-top: 20px; }

View file

@ -2,14 +2,10 @@ class HabitsController < ApplicationController
before_filter :authenticate_user!
# GET /habits
# GET /habits.json
def index
@habits = current_user.habits.where(:habit_type => Habit::ALWAYS)
@daily = current_user.habits.where(:habit_type => Habit::DAILY)
@one_time = current_user.habits.where(:habit_type => Habit::ONE_TIME).where(:done=>false)
@rewards = current_user.rewards
# TODO @rewards = current_user.rewards
@habits = current_user.habits
respond_to do |format|
format.html # index.html.erb
format.json { render json: @habits }

View file

@ -6,4 +6,4 @@
window.router = new HabitTracker.Routers.HabitsRouter({habits: <%= @habits.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
</script>