Permalink
Browse files

Project model on backbone, test case model working

  • Loading branch information...
1 parent 21fb8b4 commit 65b37d5ff32e0bd5f4355f6fdefa1abd26fb2957 @demimismo committed Sep 15, 2012
@@ -0,0 +1,3 @@
+class Smokr.Collections.Projects extends Backbone.Collection
+ url: '/api/projects'
+ model: Smokr.Models.Project
@@ -0,0 +1,2 @@
+class Smokr.Models.Project extends Backbone.Model
+ urlRoot: '/api/projects'
@@ -1,2 +1,2 @@
class Smokr.Models.TestCase extends Backbone.Model
- urlRoot: '/api/test_cases'
+ urlRoot: '/api/test_cases'
@@ -0,0 +1,15 @@
+class Smokr.Routers.Projects extends Backbone.Router
+ routes:
+ '': 'index'
+ 'projects/:id': 'show'
+
+ initialize: ->
+ @collection = new Smokr.Collections.Projects()
+ @collection.fetch()
+
+ index: ->
+ view = new Smokr.Views.ProjectsIndex(collection: @collection)
+ $('#container').html(view.render().el)
+
+ show: (id) ->
+ view = new Smokr.Views.Project()
@@ -4,7 +4,7 @@ window.Smokr =
Views: {}
Routers: {}
init: ->
- new Smokr.Routers.TestCases()
+ new Smokr.Routers.Projects()
Backbone.history.start(pushState: true)
$(document).ready ->
@@ -0,0 +1,32 @@
+class Smokr.Views.ProjectsIndex extends Backbone.View
+ template: JST['projects/index']
+
+ events:
+ 'submit #new_project': 'createProject'
+
+ initialize: ->
+ @collection.on('reset', @render, this)
+ @collection.on('add', @appendProject, this)
+
+ render: ->
+ $(@el).html(@template())
+ @collection.each(@appendProject)
+ this
+
+ appendProject: (project) =>
+ view = new Smokr.Views.Project(model: project)
+ @$('#projects').append(view.render().el)
+
+ createProject: (event) ->
+ event.preventDefault()
+ attributes = name: $('#new_project_name').val()
+ @collection.create attributes,
+ wait: true
+ success: -> $('#new_project')[0].reset()
+ error: @handleError
+
+ handleError: (project, response) ->
+ if response.status == 422
+ errors = $.parseJSON(response.responseText).errors
+ for attribute, messages of errors
+ alert "#{attribute} #{message}" for message in messages
@@ -0,0 +1,13 @@
+class Smokr.Views.Project extends Backbone.View
+ template: JST['project/project']
+ tagName: 'li'
+
+ initialize: ->
+ @model.on('change', @render, this)
+
+ showTestCase: ->
+ Backbone.history.navigate("projects/#{@model.get('id')}", true)
+
+ render: ->
+ $(@el).html(@template(project: @model))
+ this
@@ -15,7 +15,7 @@ class Smokr.Views.TestCasesIndex extends Backbone.View
appendTestCase: (test_case) =>
view = new Smokr.Views.TestCase(model: test_case)
- @$('#entries').append(view.render().el)
+ @$('#test_cases').append(view.render().el)
createTestCase: (event) ->
event.preventDefault()
@@ -0,0 +1,11 @@
+<h1>Smokr</h1>
+
+<h2>Existing projects:</h2>
+
+<ul id="projects"></ul>
+
+<h2>Add new project:</h2>
+<form id="new_project">
+ <input type="text" name="name" id="new_project_name">
+ <input type="submit" value="Add">
+</form>
@@ -0,0 +1 @@
+<%= @project.get('title') %>
@@ -6,4 +6,4 @@
<input type="submit" value="Add">
</form>
-<div id="container"></div>
+<ul id="test_cases"></ul>
@@ -1,3 +1,5 @@
class Project < ActiveRecord::Base
attr_accessible :name
+
+ validates_presence_of :name
end
@@ -1 +1 @@
-<%= content_tag "div", "Loading..", id: "container", data: { test_cases: ::TestCase.all } %>
+<div id="container"></div>

0 comments on commit 65b37d5

Please sign in to comment.