/
data-templates.coffee
49 lines (37 loc) · 1.3 KB
/
data-templates.coffee
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
do (window) ->
class Template
# We will store the template in this variable
tpl: undefined
constructor: (template_object) ->
@tpl = template_object.cloneNode(true)
# Remove the template from the dom.
template_object.parentNode.removeChild(template_object)
# Remove the id to make sure we don't create duplicates
@tpl.removeAttribute('id')
# Reset the display
@tpl.style.display = 'initial'
render: (data) ->
# Clone the template
object = @tpl.cloneNode(true)
# Get all the [date-tpl] items
items = object.querySelectorAll('[data-tpl]')
# Map data-suffixes to Element's properties
mappings =
'Html': 'innerHTML'
'Text': 'innerText'
'Href': 'href'
'Src': 'src'
'Alt': 'alt'
'Title': 'title'
# Loop through the [data-tpl] items
for item in items
# Loop through the mappings
for dataSuffix, property of mappings
# Check if there is something set to be used in the property
if val = item.dataset['tpl' + dataSuffix]
item[property] = Template.process(val, data)
return object
@process: (body, data) ->
(new Function('data', 'return ' + body))(data)
# Make it available globally
window.Template = Template