instance.coffee | |
|---|---|
| Template Instance is created for each model we are about to render.
| class Instance
constructor: (template) ->
@queryCache = {}
@childNodes = getChildNodes template
@elements = getElements template
remove: chainable ->
for node in @childNodes
node.parentNode.removeChild node
appendTo: chainable (parent) ->
for node in @childNodes
parent.appendChild node
prepare: chainable (model) ->
for element in @elements
element.reset() |
| A bit of offtopic, but let's think about writing event handlers.
It would be convenient to have an access to the associated | data(element.el).model = model |
| Rendering values takes care of the most common use cases like rendering text content, form values and DOM elements (.e.g., Backbone Views). Rendering as a text content is a safe default, as it is HTML escaped by the browsers. | renderValues: chainable (model, children) ->
if isDomElement(model) and element = @elements[0]
element.empty().el.appendChild model
else if typeof model == 'object'
for own key, value of model when value? |
| The value can be either a nested model or a plain value, i.e., | if isPlainValue value
for element in @matchingElements key |
| Element type also affects on rendering. Given a model
| element.render value |
| Rendering nested models breadth-first is more robust, as there might be colliding keys, i.e., given a model
and a template
the depth-first rendering might give us wrong results, if the children are rendered
before the
Save the key of the child model and take care of it once we're done with the parent model. | else if typeof value == 'object'
children.push key |
| With
and a model and directives
should give the result
Directives are executed after the default rendering, so that they can be used for overriding default rendering. | renderDirectives: chainable (model, index, directives) ->
for own key, attributes of directives when typeof attributes == 'object'
model = {value: model} unless typeof model == 'object'
for element in @matchingElements key
element.renderDirectives model, index, attributes
renderChildren: chainable (model, children, directives, options) ->
for key in children
for element in @matchingElements key
Transparency.render element.el, model[key], directives[key], options
matchingElements: (key) ->
elements = @queryCache[key] ||= (el for el in @elements when Transparency.matcher el, key)
log "Matching elements for '#{key}':", elements
elements
|