Permalink
Browse files

fixing episode 323

  • Loading branch information...
1 parent 6702f98 commit e80716e9b30c2db2a4aff2e62b9ddf5110a04cce @ryanb committed Feb 9, 2012
View
22 episode-323/README.md
@@ -3,3 +3,25 @@
http://railscasts.com/episodes/323-backbone-on-rails-part-1
Requires Ruby 1.9.2 or higher.
+
+Commands used in episode
+
+ bundle
+ rails new raffler
+ rails g controller main index --skip-javascripts
+ rails g backbone:install
+ rails g backbone:scaffold entry
+ rails g resource entry name winner:boolean --skip-javascripts
+ rake db:migrate
+ rake db:seed
+
+JavaScript used in console
+
+ entries = new Raffler.Collections.Entries()
+ entries.length
+ entries.fetch()
+ entry = entries.shuffle()[0]
+ entry.get('name')
+ entry.set({'winner': true})
+ entry.save()
+ entries.create({'name': 'Avdi Grimm'})
View
9 episode-323/raffler-after/app/assets/javascripts/collections/entries.js.coffee
@@ -1,11 +1,2 @@
class Raffler.Collections.Entries extends Backbone.Collection
url: '/api/entries'
-
- drawWinner: ->
- nonWinners = _.reject @shuffle(), (entry) ->
- entry.get('winner')
- winner = nonWinners[0]
- if winner
- winner.set({winner: true})
- winner.save()
- winner.trigger('highlight')
View
21 episode-323/raffler-after/app/assets/javascripts/views/entries/entries_index.js.coffee
@@ -1,28 +1,9 @@
class Raffler.Views.EntriesIndex extends Backbone.View
template: JST['entries/index']
- events:
- 'submit #new_entry': 'newEntry'
- 'click #draw': 'drawWinner'
-
initialize: ->
@collection.on('reset', @render, this)
- @collection.on('add', @addEntry, this)
-
- newEntry: (event) ->
- event.preventDefault()
- @collection.create(name: $('#new_entry_name').val())
- $('#new_entry_name').val('')
-
- drawWinner: (event) ->
- event.preventDefault()
- @collection.drawWinner()
-
- addEntry: (entry) ->
- view = new Raffler.Views.Entry(model: entry)
- $('#entries').append(view.render().el)
render: ->
- $(@el).html(@template())
- @collection.each(@addEntry)
+ $(@el).html(@template(entries: @collection))
this
View
15 episode-323/raffler-after/app/assets/javascripts/views/entries/entry.js.coffee
@@ -1,15 +0,0 @@
-class Raffler.Views.Entry extends Backbone.View
- template: JST['entries/entry']
- tagName: 'li'
-
- initialize: ->
- @model.on('change', @render, this)
- @model.on('highlight', @highlight, this)
-
- highlight: ->
- $('#entries .winner').removeClass('highlight')
- @$('.winner').addClass('highlight')
-
- render: ->
- $(@el).html(@template(entry: @model))
- this
View
4 episode-323/raffler-after/app/assets/templates/entries/entry.jst.eco
@@ -1,4 +0,0 @@
-<%= @entry.get('name') %>
-<% if @entry.get('winner'): %>
- <span class="winner">WINNER</span>
-<% end %>
View
13 episode-323/raffler-after/app/assets/templates/entries/index.jst.eco
@@ -1,10 +1,7 @@
<h1>Raffler</h1>
-<form method="post" id="new_entry">
- <input type="text" name="name" id="new_entry_name">
- <input type="submit" value="Add">
-</form>
-
-<ul id="entries"></ul>
-
-<button id="draw">Draw Winner</button>
+<ul>
+<% for entry in @entries.models: %>
+ <li><%= entry.get('name') %></li>
+<% end %>
+</ul>

0 comments on commit e80716e

Please sign in to comment.