-
Notifications
You must be signed in to change notification settings - Fork 1
/
3.coffee
40 lines (32 loc) · 920 Bytes
/
3.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
(($) ->
Item = Backbone.Model.extend
defaults:
part1: "Hello"
part2: "Word"
List = Backbone.Collection.extend
model: Item
ListView = Backbone.View.extend
el: $('body')
events:
"click button#add": 'addItem'
initialize: ->
_.bindAll(@, 'render', 'addItem', 'appendItem')
@collection = new List()
@collection.bind('add', @appendItem)
@counter = 0
@render()
render: ->
$(@el).append("<button id='add'>Add list item</button>")
$(@el).append("<ul></ul>")
_(@collection.models).each((item) ->
appendItem(item)
@)
addItem: ->
@counter++
item = new Item()
item.set part2: item.get('part2') + @counter
@collection.add(item)
appendItem: (item) ->
$('ul', @el).append("<li>"+item.get('part1')+" "+item.get('part2')+"</li>")
listView = new ListView()
)($)