-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectionbinder.coffee
More file actions
31 lines (24 loc) · 1.02 KB
/
Copy pathcollectionbinder.coffee
File metadata and controls
31 lines (24 loc) · 1.02 KB
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
define ['backbone', 'underscore'], (Backbone, _) ->
throw "Backbone's gotta be loaded first, brah" if not Backbone?
throw "Underscore is quintessential, load it first" if not _?
class CollectionBinder
constructor: (@view, @collection, @template) ->
regex = new RegExp "^[\\s\\w]*<([\\w]+)"
@topLevelElement = regex.exec(@template)[1]
bind: () ->
bindModel model for model in @collection.models
@collection.on "add", (model) =>
@bindModel model
@collection.on "remove", (model) =>
@removeModel model
uniqueId : (length=8) ->
id = ""
id += Math.random().toString(36).substr(2) while id.length < length
id.substr 0, length
bindModel: (model) ->
model.bindIdentifier = @uniqueId()
modelEl = Backbone.$(_.template @template, model.toJSON())
modelEl.attr "data-bindid", model.bindIdentifier
@view.$el.append modelEl
removeModel: (model) ->
Backbone.$(@topLevelElement + '[data-bindid="' + model.bindIdentifier + '"]').remove()