89b89668 by Michael Richards

Rewrite the each-* binder with some of the optimizations that @terrancesnyder ha…

…s implemented. [#157]
1 parent f8d23781
......@@ -368,46 +368,47 @@ Rivets.binders =
"each-*":
block: true
bind: (el, collection) ->
el.removeAttribute ['data', @view.config.prefix, @type].join('-').replace '--', '-'
unbind: (el, collection) ->
bind: (el) ->
attr = ['data', @view.config.prefix, @type].join('-').replace '--', '-'
@marker = document.createComment " rivets: #{@type} "
@iterated = []
el.removeAttribute attr
el.parentNode.insertBefore @marker, el
el.parentNode.removeChild el
unbind: (el) ->
view.unbind() for view in @iterated if @iterated?
routine: (el, collection) ->
if @iterated?
for view in @iterated
modelName = @args[0]
collection = collection or []
if @iterated.length > collection.length
for i in Array @iterated.length - collection.length
view = @iterated.pop()
view.unbind()
e.parentNode.removeChild e for e in view.els
else
@marker = document.createComment " rivets: #{@type} "
el.parentNode.insertBefore @marker, el
el.parentNode.removeChild el
@marker.parentNode.removeChild view.els[0]
@iterated = []
for model, index in collection
data = {}
data[modelName] = model
if collection
for item in collection
data = {}
data[n] = m for n, m of @view.models
data[@args[0]] = item
itemEl = el.cloneNode true
if not @iterated[index]?
for key, model of @view.models
data[key] = model
previous = if @iterated.length
@iterated[@iterated.length - 1].els[0]
else
@marker
@marker.parentNode.insertBefore itemEl, previous.nextSibling ? null
options =
binders: @view.options.binders
formatters: @view.options.binders
config: {}
options.config[k] = v for k, v of @view.options.config if @view.options.config
# Ensure preloadData is set to true since child elements won't get initiated otherwise until the next change (which might not be the first)
options.config.preloadData = true
view = new Rivets.View(itemEl, data, options)
view.bind()
@iterated.push view
template = el.cloneNode true
@iterated.push rivets.bind template, data
@marker.parentNode.insertBefore template, previous.nextSibling
else if @iterated[index].models[modelName] isnt model
@iterated[index].update data
"class-*": (el, value) ->
elClass = " #{el.className} "
......