Skip to content

Instantly share code, notes, and snippets.

@Gazler
Forked from sxross/backbone_render.coffee
Created December 8, 2011 22:38

Revisions

  1. Gary Rennie revised this gist Dec 8, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions backbone_render.coffee
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ $ ->
    @model = Song
    console.log "initializing SongsView"
    console.log @collection
    @collection.bind("fetch", @render)
    @collection.bind("reset", @render)
    @collection.fetch()

    render: =>
    @@ -41,4 +41,5 @@ $ ->
    console.log "template: #{@template}"
    $('#song-list').html @template

    songView = new SongsView(collection: songs)
    songView = new SongsView(collection: songs)
    songs.fetch()
  2. s.ross created this gist Dec 8, 2011.
    44 changes: 44 additions & 0 deletions backbone_render.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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("fetch", @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)