Confusing backbone, underscore and coffeescript
I'm a noob.
by squarism
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>
<script src="https://raw.github.com/jashkenas/coffee-script/master/extras/coffee-script.js"></script>
<script type="text/coffeescript">
####
# Template for CoffeeScript jsFiddle project with backbone and underscore library
#
# Fork me to start a new project
#
# Author: http://twitter.com/ontouchstart
# Source: http://jsfiddle.net/user/ontouchstart/
####
Log = Backbone.Model.extend
initialize: ->
@el = document.createElement 'div'
document.body.appendChild @el
print: (title) ->
t= document.createElement 'pre'
t.innerHTML = title
@el.appendChild t
# log appears to the right in "Result" for jsfiddle.net
# thanks @ontouchstart
log = new Log
entries = [
{ name: 'Joe', winner: false },
{ name: 'Bob', winner: false },
{ name: 'Henry', winner: true }
]
# this is coffeescript by itself
for entry in entries
log.print "#{entry.name} wins: #{entry.winner}"
# my app code
#non_winners = @select (entries) ->
# @get('winner') == false
# from the underscore API docs
#log.print evens = _.filter [1..6], (num) -> num % 2 is 0
# this works
non_winners = _.select entries, (entry) ->
_.get(entry).winner is true
# just testing that a block works versus one line
# log.print "in loop"
log.print "Winner is: #{non_winners[0].name}"
log.print "Great. Underscore and Coffeescript Works."
log.print "-----------"
# Let's put it in a fake backbone app
# This code isn't quite 1:1 but it's close
# for example the class below is normally
# class Raffler.Collections.Entries extends Backbone.Collection
# in it's own file but that doesn't work here
Raffler = Backbone.Collection.extend
initialize: ->
log.print "in init"
@entries = [
{ name: 'Joe', winner: false },
{ name: 'Bob', winner: false },
{ name: 'Henry', winner: true }
]
...