Adding habits scaffold

This commit is contained in:
Tyler Renelle 2012-02-07 19:09:58 -05:00
parent 7d7d7de36d
commit 1af36af298
12 changed files with 330 additions and 0 deletions

View file

@ -0,0 +1,16 @@
class HabitTracker.Models.Habit extends Backbone.Model
paramRoot: 'habit'
defaults:
name: null
habit_type: null
score: null
notes: null
up: null
down: null
done: null
position: null
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
url: '/habits'

View file

@ -0,0 +1,31 @@
class HabitTracker.Routers.HabitsRouter extends Backbone.Router
initialize: (options) ->
@habits = new HabitTracker.Collections.HabitsCollection()
@habits.reset options.habits
routes:
"/new" : "newHabit"
"/index" : "index"
"/:id/edit" : "edit"
"/:id" : "show"
".*" : "index"
newHabit: ->
@view = new HabitTracker.Views.Habits.NewView(collection: @habits)
$("#habits").html(@view.render().el)
index: ->
@view = new HabitTracker.Views.Habits.IndexView(habits: @habits)
$("#habits").html(@view.render().el)
show: (id) ->
habit = @habits.get(id)
@view = new HabitTracker.Views.Habits.ShowView(model: habit)
$("#habits").html(@view.render().el)
edit: (id) ->
habit = @habits.get(id)
@view = new HabitTracker.Views.Habits.EditView(model: habit)
$("#habits").html(@view.render().el)

View file

@ -0,0 +1,50 @@
<h1>Edit habit</h1>
<form id="edit-habit" name="habit">
<div class="field">
<label for="name"> name:</label>
<input type="text" name="name" id="name" value="<%= name %>" >
</div>
<div class="field">
<label for="habit_type"> habit_type:</label>
<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 %>" >
</div>
<div class="field">
<label for="up"> up:</label>
<input type="text" name="up" id="up" value="<%= up %>" >
</div>
<div class="field">
<label for="down"> down:</label>
<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>
</form>
<a href="#/index">Back</a>

View file

@ -0,0 +1,12 @@
<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>

View file

@ -0,0 +1,21 @@
<h1>Listing habits</h1>
<table id="habits-table">
<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>
</tr>
</table>
<br/>
<a href="#/new">New Habit</a>

View file

@ -0,0 +1,50 @@
<h1>New habit</h1>
<form id="new-habit" name="habit">
<div class="field">
<label for="name"> name:</label>
<input type="text" name="name" id="name" value="<%= name %>" >
</div>
<div class="field">
<label for="habit_type"> habit_type:</label>
<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 %>" >
</div>
<div class="field">
<label for="up"> up:</label>
<input type="text" name="up" id="up" value="<%= up %>" >
</div>
<div class="field">
<label for="down"> down:</label>
<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>
</form>
<a href="#/index">Back</a>

View file

@ -0,0 +1,42 @@
<p>
<b>Name:</b>
<%= name %>
</p>
<p>
<b>Habit type:</b>
<%= habit_type %>
</p>
<p>
<b>Score:</b>
<%= score %>
</p>
<p>
<b>Notes:</b>
<%= notes %>
</p>
<p>
<b>Up:</b>
<%= up %>
</p>
<p>
<b>Down:</b>
<%= down %>
</p>
<p>
<b>Done:</b>
<%= done %>
</p>
<p>
<b>Position:</b>
<%= position %>
</p>
<a href="#/index">Back</a>

View file

@ -0,0 +1,24 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.EditView extends Backbone.View
template : JST["backbone/templates/habits/edit"]
events :
"submit #edit-habit" : "update"
update : (e) ->
e.preventDefault()
e.stopPropagation()
@model.save(null,
success : (habit) =>
@model = habit
window.location.hash = "/#{@model.id}"
)
render : ->
$(@el).html(@template(@model.toJSON() ))
this.$("form").backboneLink(@model)
return this

View file

@ -0,0 +1,19 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.HabitView extends Backbone.View
template: JST["backbone/templates/habits/habit"]
events:
"click .destroy" : "destroy"
tagName: "tr"
destroy: () ->
@model.destroy()
this.remove()
return false
render: ->
$(@el).html(@template(@model.toJSON() ))
return this

View file

@ -0,0 +1,20 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.IndexView extends Backbone.View
template: JST["backbone/templates/habits/index"]
initialize: () ->
@options.habits.bind('reset', @addAll)
addAll: () =>
@options.habits.each(@addOne)
addOne: (habit) =>
view = new HabitTracker.Views.Habits.HabitView({model : habit})
@$("tbody").append(view.render().el)
render: =>
$(@el).html(@template(habits: @options.habits.toJSON() ))
@addAll()
return this

View file

@ -0,0 +1,37 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.NewView extends Backbone.View
template: JST["backbone/templates/habits/new"]
events:
"submit #new-habit": "save"
constructor: (options) ->
super(options)
@model = new @collection.model()
@model.bind("change:errors", () =>
this.render()
)
save: (e) ->
e.preventDefault()
e.stopPropagation()
@model.unset("errors")
@collection.create(@model.toJSON(),
success: (habit) =>
@model = habit
window.location.hash = "/#{@model.id}"
error: (habit, jqXHR) =>
@model.set({errors: $.parseJSON(jqXHR.responseText)})
)
render: ->
$(@el).html(@template(@model.toJSON() ))
this.$("form").backboneLink(@model)
return this

View file

@ -0,0 +1,8 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.ShowView extends Backbone.View
template: JST["backbone/templates/habits/show"]
render: ->
$(@el).html(@template(@model.toJSON() ))
return this