Skip to content

Instantly share code, notes, and snippets.

@chiller
Last active December 14, 2015 01:29
Show Gist options
  • Save chiller/5006289 to your computer and use it in GitHub Desktop.
Save chiller/5006289 to your computer and use it in GitHub Desktop.
Cheatsheet: skeleton for a small backbone exampleapp
class NestedCommentView extends Backbone.View
tagname: "div"
initialize: =>
@model.bind 'change', @render
@model.bind 'destroy', @unrender
#bind this to a dom event
#events:
# "click .deletecomment": "deleteComment"
deleteComment: =>
@model.destroy()
render: =>
$(@el).html "<p>Test #{@model.get 'name'}</p>"
@
unrender: =>
$(@el).remove()
@
class TestModel extends Backbone.Model
render: =>
myview = new NestedCommentView model: @
$("body").append myview.render().el
class CommentList extends Backbone.Collection
model: TestModel
initialize: =>
@bind 'add', (item) -> item.render()
$ ->
window.commentCollection = new CommentList
window.commentCollection.add([
new TestModel name:"alma",
new TestModel name:"korte",
new TestModel name:"birs",
])
window.commentCollection.models[0].set("name","banana")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment