forked from jenimech/Testing-backbone-with-coffescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.coffee
60 lines (55 loc) · 1.76 KB
/
5.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
(($) ->
Backbone.sync = (method, model, success, error) -> success()
Item = Backbone.Model.extend
defaults:
part1: "Hello"
part2: "world"
List = Backbone.Collection.extend
model: Item
ItemView = Backbone.View.extend
tagName: 'li'
events:
"click span.swap": 'swap'
"click span.delete": 'remove'
initialize: ->
_.bindAll(@, 'render', 'unrender', 'swap', 'remove')
@model.bind('change', @render)
@model.bind('remove', @unrender)
render: ->
$(@el).html('<span style="color:black;">'[email protected]('part1')+' '[email protected]('part2')+'</span> <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
return @
unrender: ->
$(@el).remove()
swap: ->
swapped =
part1: @model.get('part2')
part2: @model.get('part1')
@model.set(swapped)
remove: ->
@model.destroy()
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) ->
itemView = new ItemView model: item
$('ul', @el).append(itemView.render().el)
listView = new ListView()
)($)