-
Notifications
You must be signed in to change notification settings - Fork 310
/
rivets.coffee
89 lines (72 loc) · 2.24 KB
/
rivets.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
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
# The Rivets namespace.
Rivets =
options: [
'prefix'
'templateDelimiters'
'rootInterface'
'preloadData'
'handler',
'executeFunctions'
]
extensions: [
'binders'
'formatters'
'components'
'adapters'
]
# The public interface (this is the exported module object).
public:
# Global binders.
binders: {}
# Global components.
components: {}
# Global formatters.
formatters: {}
# Global sightglass adapters.
adapters: {}
# Default attribute prefix.
prefix: 'rv'
# Default template delimiters.
templateDelimiters: ['{', '}']
# Default sightglass root interface.
rootInterface: '.'
# Preload data by default.
preloadData: true,
# Execute functions in bindings. Defaultis false since rivets 0.9. Set to true to be backward compatible with rivets 0.8.
executeFunctions: false,
# Alias for index in rv-each binder
iterationAlias : (modelName) ->
return '%' + modelName + '%'
# Default event handler.
handler: (context, ev, binding) ->
@call context, ev, binding.view.models
# Merges an object literal into the corresponding global options.
configure: (options = {}) ->
for option, value of options
if option in ['binders', 'components', 'formatters', 'adapters']
for key, descriptor of value
Rivets[option][key] = descriptor
else
Rivets.public[option] = value
return
# Binds some data to a template / element. Returns a Rivets.View instance.
bind: (el, models = {}, options = {}) ->
view = new Rivets.View(el, models, options)
view.bind()
view
# Initializes a new instance of a component on the specified element and
# returns a Rivets.View instance.
init: (component, el, data = {}) ->
el ?= document.createElement 'div'
component = Rivets.public.components[component]
template = component.template.call @, el
if template instanceof HTMLElement
while el.firstChild
el.removeChild(el.firstChild)
el.appendChild(template)
else
el.innerHTML = template
scope = component.initialize.call @, el, data
view = new Rivets.View(el, scope)
view.bind()
view