Skip to content

Instantly share code, notes, and snippets.

@Gazler
Forked from sxross/backbone_render.coffee
Created December 8, 2011 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gazler/1449022 to your computer and use it in GitHub Desktop.
Save Gazler/1449022 to your computer and use it in GitHub Desktop.
Backbone render
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment