forked from leonidas/transparency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransparency.coffee
More file actions
99 lines (86 loc) · 3.32 KB
/
transparency.coffee
File metadata and controls
99 lines (86 loc) · 3.32 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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
_ = require '../lib/lodash.js'
helpers = require './helpers'
Context = require './context'
# **Transparency** is a client-side template engine which binds JSON objects to DOM elements.
#
# // Template:
# //
# // <ul id="todos">
# // <li class="todo"></li>
# // </ul>
#
# template = document.querySelector('#todos');
#
# models = [
# {todo: "Eat"},
# {todo: "Do some programming"},
# {todo: "Sleep"}
# ];
#
# Transparency.render(template, models);
#
# // Result:
# // <ul id="todos">
# // <li class="todo">Eat</li>
# // <li class="todo">Do some programming</li>
# // <li class="todo">Sleep</li>
# // </ul>
#
# This documentation focuses on the implementation and internals.
# For the full API reference, please see the README.
# ## Public API
Transparency = {}
# `Transparency.render` maps JSON objects to DOM elements.
Transparency.render = (context, models = [], directives = {}, options = {}) ->
# First, check if we are in debug mode and if so, log the arguments.
log = if options.debug and console then helpers.consoleLogger else helpers.nullLogger
log "Transparency.render:", context, models, directives, options
return unless context
models = [models] unless _.isArray models
# Context element, state and functionality is wrapped to `Context` object. Get it, or create a new
# if it doesn't exist yet.
context = helpers.data(context).context ||= new Context(context, Transparency)
# Rendering is a lot faster when the context element is detached from the DOM, as
# reflow calculations are not triggered. So, detach it before rendering.
context.render(models, directives, options).el
# ### Configuration
# By default, Transparency matches model properties to elements by `id`, `class`, `name` and `data-bind` attributes.
# Override `Transparency.matcher` to change the default behavior.
#
# // Match only by `data-bind` attribute
# Transparency.matcher = function (element, key) {
# element.el.getAttribute('data-bind') == key;
# };
#
Transparency.matcher = (element, key) ->
element.el.id == key ||
key in element.classNames ||
element.el.name == key ||
element.el.getAttribute('data-bind') == key
# IE6-8 fails to clone nodes properly. By default, Transparency uses jQuery.clone() as a shim.
# Override `Transparency.clone` with a custom clone function, if oldIE needs to be
# supported without jQuery.
#
# Transparency.clone = myCloneFunction;
#
Transparency.clone = (node) ->
$(node).clone()[0]
# ### Exports
# In order to use Transparency as a jQuery plugin, add Transparency.jQueryPlugin to jQuery.fn object.
#
# $.fn.render = Transparency.jQueryPlugin;
#
# // Render with jQuery
# $('#template').render({hello: 'World'});
#
Transparency.jQueryPlugin = helpers.chainable (models, directives, options) ->
for context in this
Transparency.render context, models, directives, options
# Register Transparency, if jQuery or Zepto is defined
if (jQuery? || Zepto?)
$ = jQuery || Zepto
$?.fn.render = Transparency.jQueryPlugin
# Exports for node.js, browser global and AMD
if module?.exports then module.exports = Transparency
if window? then window.Transparency = Transparency
if define?.amd then define -> Transparency