public
Created

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 46
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"
 
window.songs = new Songs
 
class SongsView extends Backbone.View
initialize: ->
@el = 'body'
@model = Song
@collection.bind("reset", @render)
@collection.fetch()
@events = {'click button': 'zeep'}
@delegateEvents()
@render()
 
render: (arg) =>
console.log "render"
console.log @collection
template = Handlebars.compile(SONG_TEMPLATE)
@template = template(songs: @collection.toJSON())
console.log "template: #{@template}"
$('#song-list').html @template
zeep: =>
console.log 'zeep!'
@collection.fetch()
window.songView = new SongsView(collection: window.songs)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.