PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/coffee/helloworld3.coffee

https://bitbucket.org/digitaltrike/backbonejs-hello-world
CoffeeScript | 42 lines | 29 code | 10 blank | 3 comment | 0 complexity | 3b56c0f0c3baf6e79a9acf7a84041800 MD5 | raw file
  1. # Model
  2. window.Item = Backbone.Model.extend(defaults:
  3. part1: "hello"
  4. part2: "world"
  5. )
  6. # Collection
  7. window.List = Backbone.Collection.extend(model: Item)
  8. # View
  9. window.ListView = Backbone.View.extend(
  10. events:
  11. "click button#add": "addItem"
  12. initialize: ->
  13. _.bindAll @
  14. @collection = new List()
  15. @collection.bind "add", @appendItem # collection event binder
  16. @counter = 0
  17. @render()
  18. render: ->
  19. self = @ # Preserve context
  20. @$el.html _.template $("#template_helloworld2").html(), {}
  21. @collection.each (item) ->
  22. self.appendItem item
  23. , @
  24. $(".backbone").html @$el
  25. addItem: ->
  26. @counter++
  27. item = new Item()
  28. item.set part2: item.get("part2") + @counter # modify item defaults
  29. @collection.add item # add item to collection; view is updated via event 'add'
  30. appendItem: (item) ->
  31. $("ul", @$el).append "<li>" + item.get("part1") + " " + item.get("part2") + "</li>"
  32. )