public
Created — forked from sxross/backbone_render.coffee

Backbone render

  • Download Gist
backbone_render.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
SONG_TEMPLATE = '''
<table>
{{#if songs.length }}
{{#each songs}}
<tr><td>{{ this.name }}</td><td>{{ this.duration }}</td></tr>
{{/each}}
{{/if}}
</table>
'''
 
$ ->
class Song extends Backbone.Model
parse: (response) ->
console.log "model parsing #{response}"
#
 
class Songs extends Backbone.Collection
initialize: ->
@model = Song
@url = "/songs/data"
 
parse: (response) ->
console.log "collection parsing"
console.log response
 
songs = new Songs
 
class SongsView extends Backbone.View
initialize: ->
@model = Song
console.log "initializing SongsView"
console.log @collection
@collection.bind("reset", @render)
@collection.fetch()
 
render: =>
console.log "render"
console.log @collection
template = Handlebars.compile(SONG_TEMPLATE)
@template = template(songs: @collection.toJSON())
console.log "template: #{@template}"
$('#song-list').html @template
songView = new SongsView(collection: songs)
songs.fetch()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.