context.coffee | |
|---|---|
| Context stores the original | class Context
detach = chainable ->
@parent = @el.parentNode
if @parent
@nextSibling = @el.nextSibling
@parent.removeChild @el
attach = chainable ->
if @parent
if @nextSibling
then @parent.insertBefore @el, @nextSibling
else @parent.appendChild @el
constructor: (@el) ->
@template = cloneNode @el
@instances = [new Instance(@el)]
@instanceCache = []
render: \
before(detach) \
after(attach) \
chainable \
(models, directives, options) -> |
| Cloning DOM elements is expensive, so save unused template | while models.length < @instances.length
@instanceCache.push @instances.pop().remove()
for model, index in models
unless instance = @instances[index]
instance = @instanceCache.pop() || new Instance(cloneNode(@template))
@instances.push instance.appendTo(@el)
children = []
instance
.prepare(model, children)
.renderValues(model, children)
.renderDirectives(model, index, directives)
.renderChildren(model, children, directives, options)
|