Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

single thread now displays using backbone.js, passes cucumber feature.

  • Loading branch information...
commit 69aa1c61c0cc59ce22bc286ce8e0e9e0e7ecdde3 1 parent 03d31b2
Chris Salzberg shioyama authored
10 app/assets/javascripts/cojiro_app.js.coffee
View
@@ -3,6 +3,10 @@ window.CojiroApp =
Collections: {}
Views: {}
Routers: {}
- init: ->
- new CojiroApp.Routers.Cothreads()
- Backbone.history.start()
+ init: (data) ->
+ @cothread = new CojiroApp.Models.Cothread(data.cothread)
+
+ new CojiroApp.Routers.Cothreads(model: @cothread)
+ if (!Backbone.history.started)
+ Backbone.history.start()
+ Backbone.history.started = true
3  app/assets/javascripts/collections/cothreads.js.coffee
View
@@ -0,0 +1,3 @@
+class CojiroApp.Collections.Cothreads extends Backbone.Collection
+ model: CojiroApp.Models.Cothread
+ url: '/cothreads'
4 app/assets/javascripts/models/cothread.js.coffee
View
@@ -1,9 +1,9 @@
class CojiroApp.Models.Cothread extends Backbone.Model
url: ->
if @isNew()
- '/threads.json'
+ '/threads'
else
- '/threads/' + @getId() + '.json'
+ '/threads/' + @getId()
defaults:
title: ''
7 app/assets/javascripts/routers/cothreads.js.coffee
View
@@ -0,0 +1,7 @@
+class CojiroApp.Routers.Cothreads extends Backbone.Router
+ routes:
+ "" : "show"
+
+ show: ->
+ view = new CojiroApp.Views.CothreadsShow( model: CojiroApp.cothread )
+ $('.content').html(view.render().el)
2  app/assets/javascripts/views/cothreads/cothreads_show.js.coffee
View
@@ -2,6 +2,8 @@ class CojiroApp.Views.CothreadsShow extends Backbone.View
id: 'thread'
initialize: ->
+ _.bindAll @
+ @render()
render: ->
@$el.html(JST['cothreads/show'](model: @model))
2  app/controllers/cothreads_controller.rb
View
@@ -1,8 +1,10 @@
class CothreadsController < ApplicationController
before_filter :login_required, :except => [:show]
before_filter :find_cothread, :except => [:new, :create]
+ respond_to :html, :json
def show
+ respond_with(@cothread)
end
def new
15 app/views/cothreads/show.html.haml
View
@@ -1,5 +1,14 @@
- title @cothread.title
-.page-header
- %h1
-
#thread
+
+%script#bootstrap{ type: "text/json" }
+ { "cothread":
+ =@cothread.to_json.html_safe
+ }
+
+=content_for :javascript do
+ %script{ type: "text/javascript" }
+ $(function () {
+ data = JSON.parse($('#bootstrap').text());
+ CojiroApp.init(data);
+ });
1  app/views/layouts/application.html.haml
View
@@ -30,3 +30,4 @@
- flash.each do |name, msg|
= content_tag :div, '<a class="close" href="#">×</a>'.html_safe + msg, :class => "alert alert-#{name} #{name}", :id => "flash_#{name}", :'data-dismiss' => 'alert' if msg.is_a?(String)
= yield
+ = yield :javascript
6 features/step_definitions/interaction_steps.rb
View
@@ -17,3 +17,9 @@
Then /^I (should|should not) see the text:? "([^"]*)"$/ do |expectation, text|
page.send(expectation.gsub(' ','_'),have_content(text))
end
+
+Then /^I (should|should not) see the text:? "([^"]*)" in the thread$/ do |expectation, text|
+ within(:css, '#thread') do
+ page.send(expectation.gsub(' ','_'),have_content(text))
+ end
+end
6 features/thread_view.feature
View
@@ -10,12 +10,12 @@ Feature: View a thread
| nickname | csasaki |
And my locale is "en"
+ @javascript
Scenario: View a thread
Given the following thread exists:
| user | csasaki |
| title | Co-working spaces in Tokyo |
| summary | I want to write an an article about the increased popularity of co-working spaces. |
And I am on the page for the thread "Co-working spaces in Tokyo"
- Then I should see the text "Co-working spaces in Tokyo"
- And I should see the text "I want to write an an article about the increased popularity of co-working spaces."
-
+ Then I should see the text "Co-working spaces in Tokyo" in the thread
+ And I should see the text "I want to write an an article about the increased popularity of co-working spaces." in the thread
22 spec/javascripts/cojiro_app_spec.js.coffee
View
@@ -0,0 +1,22 @@
+describe "CojiroApp", ->
+ it "has a namespace for Models", -> expect(CojiroApp.Models).toBeTruthy()
+ it "has a namespace for Collection", -> expect(CojiroApp.Collections).toBeTruthy()
+ it "has a namespace for Views", -> expect(CojiroApp.Views).toBeTruthy()
+ it "has a namespace for Routers", -> expect(CojiroApp.Routers).toBeTruthy()
+
+ describe "init()", ->
+ it "accepts JSON and instantiates collections from it", ->
+ cothreadsJSON =
+ "cothread":
+ "title": "Co-working spaces in Tokyo","summary": "I'm collecting blog posts on co-working spaces in Tokyo."
+
+ CojiroApp.init(cothreadsJSON)
+
+ expect(CojiroApp.cothread).not.toEqual(undefined)
+ expect(CojiroApp.cothread.getTitle()).toEqual("Co-working spaces in Tokyo")
+ expect(CojiroApp.cothread.getSummary()).toEqual("I'm collecting blog posts on co-working spaces in Tokyo.")
+
+ it "instantiates a Cothreads router", ->
+ CojiroApp.Routers.Cothreads = sinon.spy()
+ CojiroApp.init({})
+ expect(CojiroApp.Routers.Cothreads).toHaveBeenCalled()
8 spec/javascripts/collections/cothreads_spec.js.coffee
View
@@ -0,0 +1,8 @@
+describe "CojiroApp.Collections.Cothreads", ->
+ it "contains instances of CojiroApp.Models.Cothread", ->
+ collection = new CojiroApp.Collections.Cothreads()
+ expect(collection.model).toEqual(CojiroApp.Models.Cothread)
+
+ it "is persisted at /threads", ->
+ collection = new CojiroApp.Collections.Cothreads()
+ expect(collection.url).toEqual("/cothreads")
4 spec/javascripts/models/cothread_spec.js.coffee
View
@@ -69,7 +69,7 @@ describe 'CojiroApp.Models.Cothread', ->
it 'is a POST', -> expect(@request).toBePOST()
it 'is async', -> expect(@request).toBeAsync()
- it 'has a valid URL', -> expect(@request).toHaveUrl('/threads.json')
+ it 'has a valid URL', -> expect(@request).toHaveUrl('/threads')
describe 'on update', ->
beforeEach ->
@@ -79,4 +79,4 @@ describe 'CojiroApp.Models.Cothread', ->
it 'is a PUT', -> expect(@request).toBePUT()
it 'is async', -> expect(@request).toBeAsync()
- it 'has a valid URL', -> expect(@request).toHaveUrl('/threads/66.json')
+ it 'has a valid URL', -> expect(@request).toHaveUrl('/threads/66')
Please sign in to comment.
Something went wrong with that request. Please try again.