rivets.coffee
2.21 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
# rivets.js
# version : 0.1.0
# author : Michael Richards
# license : MIT
window.rivets = do ->
registerBinding = (el, interface, type, context, keypath) ->
bindings[type] el, interface.read(context, keypath)
interface.subscribe context, keypath, (value) ->
bindings[type] el, value
if inputValue = getInputValue el
interface.publish context, keypath, inputValue
setAttribute = (el, attr, value, mirrored=false) ->
if value
$(el).attr attr, if mirrored then attr else value
else
$(el).removeAttr attr
getInputValue = (el) ->
switch $(el).attr 'type'
when 'text', 'textarea', 'password', 'select-one' then $(this).val()
when 'checkbox' then $(this).is ':checked'
bindings =
show: (el, value) ->
if value then $(el).show() else $(el).hide()
hide: (el, value) ->
if value then $(el).hide() else $(el).show()
enabled: (el, value) ->
setAttribute el, 'disabled', !value, true
disabled: (el, value) ->
setAttribute el, 'disabled', value, true
checked: (el, value) ->
setAttribute el, 'checked', value, true
unchecked: (el, value) ->
setAttribute el, 'checked', !value, true
selected: (el, value) ->
setAttribute el, 'selected', value, true
unselected: (el, value) ->
setAttribute el, 'checked', !value, true
text: (el, value) ->
$(el).text value or ''
value: (el, value) ->
$(el).val value
bindableAttributes = ['id', 'class', 'name', 'src', 'href', 'alt', 'title', 'placeholder']
for attr in bindableAttributes
do (attr) ->
bindings[attr] = (el, value) ->
setAttribute el, attr, value
bind: (el, interface, contexts={}) ->
$(el).add($('*', el)).each ->
target = this
nodeMap = target.attributes
if nodeMap.length > 0
[0..(nodeMap.length - 1)].forEach (n) ->
node = nodeMap[n]
if /^data-/.test node.name
type = node.name.replace 'data-', ''
if _.include _.keys(bindings), type
path = node.value.split '.'
context = path.shift()
keypath = path.join '.'
registerBinding $(target), interface, type, contexts[context], keypath