forked from gijs/hello-backbonejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.coffee
66 lines (43 loc) · 1.49 KB
/
4.coffee
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# **This example illustrates how to delegate the rendering of a Model to a dedicated View.**
#
# _Working example: [4.html](../4.html)._
# _[Go to Example 5](5.html)_
#
jQuery ->
class Item extends Backbone.Model
defaults:
part1: 'hello'
part2: 'world'
class List extends Backbone.Collection
model: Item
# **ItemView class**: Responsible for rendering each individual `Item`.
class ItemView extends Backbone.View
tagName: 'li'
initialize: ->
_.bindAll @
render: ->
$(@el).html "<span>#{@model.get 'part1'} #{@model.get 'part2'}</span>"
@
class ListView extends Backbone.View
el: $ 'body'
initialize: ->
_.bindAll @
@collection = new List
@collection.bind 'add', @appendItem
@counter = 0
@render()
render: ->
$(@el).append '<button>Add List Item</button>'
$(@el).append '<ul></ul>'
_.each @collection.models, (item) -> appendItem item
addItem: ->
@counter++
item = new Item
item.set part2: "#{item.get 'part2'} #{@counter}"
@collection.add item
# `appendItem()` is no longer responsible for rendering an individual `Item`. This is now delegated to the `render()` method of each `ItemView` instance.
appendItem: (item) ->
itemView = new ItemView model: item
$('ul').append itemView.render().el
events: 'click button': 'addItem'
listView = new ListView