3fe45ff0 by Michael Richards

Add comments to source.

1 parent fe9918e1
1 # rivets.js 1 # rivets.js
2 # version : 0.1.2 2 # version : 0.1.2
3 # author : Michael Richards 3 # author : Michael Richards
4 # license : MIT 4 # license : MIT
5 5
6 # Registers a specific binding routine between a model object and a DOM element.
7 # All information for that routine is passed in when calling this function; the
8 # specific element to bind to, an adapter interface for the model object, the
9 # type of binding, the context object and the keypath at which to subscribe to
10 # on the model object.
6 registerBinding = (el, adapter, type, context, keypath) -> 11 registerBinding = (el, adapter, type, context, keypath) ->
7 bind = bindings[type] || attributeBinding type 12 bind = bindings[type] || attributeBinding type
8 bind el, adapter.read context, keypath 13 bind el, adapter.read context, keypath
...@@ -14,17 +19,24 @@ registerBinding = (el, adapter, type, context, keypath) -> ...@@ -14,17 +19,24 @@ registerBinding = (el, adapter, type, context, keypath) ->
14 el.addEventListener 'change', -> 19 el.addEventListener 'change', ->
15 adapter.publish context, keypath, getInputValue this 20 adapter.publish context, keypath, getInputValue this
16 21
22 # Returns the current input value for the specified element.
17 getInputValue = (el) -> 23 getInputValue = (el) ->
18 switch el.type 24 switch el.type
19 when 'text', 'textarea', 'password', 'select-one' then el.value 25 when 'text', 'textarea', 'password', 'select-one' then el.value
20 when 'checkbox', 'radio' then el.checked 26 when 'checkbox', 'radio' then el.checked
21 27
28 # Returns an attribute binding routine for the specified attribute. This is what
29 # `registerBinding` falls back to when there is no routine for the binding type.
22 attributeBinding = (attr) -> (el, value) -> 30 attributeBinding = (attr) -> (el, value) ->
23 if value then el.setAttribute attr, value else el.removeAttribute attr 31 if value then el.setAttribute attr, value else el.removeAttribute attr
24 32
33 # Returns a state binding routine for the specified attribute. Can optionally be
34 # negatively evaluated. This is used to build a lot of the core state binding
35 # routines.
25 stateBinding = (attr, inverse = false) -> (el, value) -> 36 stateBinding = (attr, inverse = false) -> (el, value) ->
26 attributeBinding(attr) el, if inverse is !value then attr else false 37 attributeBinding(attr) el, if inverse is !value then attr else false
27 38
39 # Core binding routines.
28 bindings = 40 bindings =
29 checked: 41 checked:
30 stateBinding 'checked' 42 stateBinding 'checked'
...@@ -49,8 +61,11 @@ bindings = ...@@ -49,8 +61,11 @@ bindings =
49 hide: (el, value) -> 61 hide: (el, value) ->
50 el.style.display = if value then 'none' else '' 62 el.style.display = if value then 'none' else ''
51 63
64 # Bindings that should also be observed for changes on the DOM element in order
65 # to propogate those changes back to the model object.
52 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'] 66 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
53 67
68 # The rivets module. Exposes a single `bind` function.
54 rivets = 69 rivets =
55 bind: (el, adapter, contexts = {}) -> 70 bind: (el, adapter, contexts = {}) ->
56 for node in el.getElementsByTagName '*' 71 for node in el.getElementsByTagName '*'
...@@ -62,6 +77,7 @@ rivets = ...@@ -62,6 +77,7 @@ rivets =
62 keypath = path.join '.' 77 keypath = path.join '.'
63 registerBinding node, adapter, type, contexts[context], keypath 78 registerBinding node, adapter, type, contexts[context], keypath
64 79
80 # Exports rivets for both CommonJS and the browser.
65 if module? 81 if module?
66 module.exports = rivets 82 module.exports = rivets
67 else 83 else
......