Export library for both CommonJS and the browser.
Showing
2 changed files
with
27 additions
and
12 deletions
1 | // Generated by CoffeeScript 1.3.1 | 1 | // Generated by CoffeeScript 1.3.1 |
2 | (function() { | 2 | (function() { |
3 | var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | 3 | var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, rivets, setAttribute, |
4 | __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
4 | 5 | ||
5 | window.rivets = (function() { | ||
6 | var attributeBinding, bidirectionalBindings, bindings, getInputValue, registerBinding, setAttribute; | ||
7 | registerBinding = function(el, adapter, type, context, keypath) { | 6 | registerBinding = function(el, adapter, type, context, keypath) { |
8 | var bind; | 7 | var bind; |
9 | bind = bindings[type] || attributeBinding(type); | 8 | bind = bindings[type] || attributeBinding(type); |
... | @@ -17,6 +16,7 @@ | ... | @@ -17,6 +16,7 @@ |
17 | }); | 16 | }); |
18 | } | 17 | } |
19 | }; | 18 | }; |
19 | |||
20 | setAttribute = function(el, attr, value, mirrored) { | 20 | setAttribute = function(el, attr, value, mirrored) { |
21 | if (mirrored == null) { | 21 | if (mirrored == null) { |
22 | mirrored = false; | 22 | mirrored = false; |
... | @@ -27,6 +27,7 @@ | ... | @@ -27,6 +27,7 @@ |
27 | return el.removeAttribute(attr); | 27 | return el.removeAttribute(attr); |
28 | } | 28 | } |
29 | }; | 29 | }; |
30 | |||
30 | getInputValue = function(el) { | 31 | getInputValue = function(el) { |
31 | switch (el.type) { | 32 | switch (el.type) { |
32 | case 'text': | 33 | case 'text': |
... | @@ -38,6 +39,7 @@ | ... | @@ -38,6 +39,7 @@ |
38 | return el.checked; | 39 | return el.checked; |
39 | } | 40 | } |
40 | }; | 41 | }; |
42 | |||
41 | bindings = { | 43 | bindings = { |
42 | show: function(el, value) { | 44 | show: function(el, value) { |
43 | return el.style.display = value ? '' : 'none'; | 45 | return el.style.display = value ? '' : 'none'; |
... | @@ -70,13 +72,16 @@ | ... | @@ -70,13 +72,16 @@ |
70 | return el.value = value; | 72 | return el.value = value; |
71 | } | 73 | } |
72 | }; | 74 | }; |
75 | |||
73 | attributeBinding = function(attr) { | 76 | attributeBinding = function(attr) { |
74 | return function(el, value) { | 77 | return function(el, value) { |
75 | return setAttribute(el, attr, value); | 78 | return setAttribute(el, attr, value); |
76 | }; | 79 | }; |
77 | }; | 80 | }; |
81 | |||
78 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']; | 82 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']; |
79 | return { | 83 | |
84 | rivets = { | ||
80 | bind: function(el, adapter, contexts) { | 85 | bind: function(el, adapter, contexts) { |
81 | var nodes, _i, _ref, _results; | 86 | var nodes, _i, _ref, _results; |
82 | if (contexts == null) { | 87 | if (contexts == null) { |
... | @@ -110,6 +115,11 @@ | ... | @@ -110,6 +115,11 @@ |
110 | }); | 115 | }); |
111 | } | 116 | } |
112 | }; | 117 | }; |
113 | })(); | 118 | |
119 | if (typeof module !== "undefined" && module !== null) { | ||
120 | module.exports = rivets; | ||
121 | } else { | ||
122 | this.rivets = rivets; | ||
123 | } | ||
114 | 124 | ||
115 | }).call(this); | 125 | }).call(this); | ... | ... |
... | @@ -3,8 +3,7 @@ | ... | @@ -3,8 +3,7 @@ |
3 | # author : Michael Richards | 3 | # author : Michael Richards |
4 | # license : MIT | 4 | # license : MIT |
5 | 5 | ||
6 | window.rivets = do -> | 6 | registerBinding = (el, adapter, type, context, keypath) -> |
7 | registerBinding = (el, adapter, type, context, keypath) -> | ||
8 | bind = bindings[type] || attributeBinding type | 7 | bind = bindings[type] || attributeBinding type |
9 | bind el, adapter.read context, keypath | 8 | bind el, adapter.read context, keypath |
10 | 9 | ||
... | @@ -15,18 +14,18 @@ window.rivets = do -> | ... | @@ -15,18 +14,18 @@ window.rivets = do -> |
15 | el.addEventListener 'change', -> | 14 | el.addEventListener 'change', -> |
16 | adapter.publish context, keypath, getInputValue this | 15 | adapter.publish context, keypath, getInputValue this |
17 | 16 | ||
18 | setAttribute = (el, attr, value, mirrored=false) -> | 17 | setAttribute = (el, attr, value, mirrored=false) -> |
19 | if value | 18 | if value |
20 | el.setAttribute attr, if mirrored then attr else value | 19 | el.setAttribute attr, if mirrored then attr else value |
21 | else | 20 | else |
22 | el.removeAttribute attr | 21 | el.removeAttribute attr |
23 | 22 | ||
24 | getInputValue = (el) -> | 23 | getInputValue = (el) -> |
25 | switch el.type | 24 | switch el.type |
26 | when 'text', 'textarea', 'password', 'select-one' then el.value | 25 | when 'text', 'textarea', 'password', 'select-one' then el.value |
27 | when 'checkbox' then el.checked | 26 | when 'checkbox' then el.checked |
28 | 27 | ||
29 | bindings = | 28 | bindings = |
30 | show: (el, value) -> | 29 | show: (el, value) -> |
31 | el.style.display = if value then '' else 'none' | 30 | el.style.display = if value then '' else 'none' |
32 | hide: (el, value) -> | 31 | hide: (el, value) -> |
... | @@ -48,12 +47,13 @@ window.rivets = do -> | ... | @@ -48,12 +47,13 @@ window.rivets = do -> |
48 | value: (el, value) -> | 47 | value: (el, value) -> |
49 | el.value = value | 48 | el.value = value |
50 | 49 | ||
51 | attributeBinding = (attr) -> | 50 | attributeBinding = (attr) -> |
52 | (el, value) -> | 51 | (el, value) -> |
53 | setAttribute el, attr, value | 52 | setAttribute el, attr, value |
54 | 53 | ||
55 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'] | 54 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'] |
56 | 55 | ||
56 | rivets = | ||
57 | bind: (el, adapter, contexts={}) -> | 57 | bind: (el, adapter, contexts={}) -> |
58 | nodes = el.getElementsByTagName '*' | 58 | nodes = el.getElementsByTagName '*' |
59 | 59 | ||
... | @@ -70,3 +70,8 @@ window.rivets = do -> | ... | @@ -70,3 +70,8 @@ window.rivets = do -> |
70 | context = path.shift() | 70 | context = path.shift() |
71 | keypath = path.join '.' | 71 | keypath = path.join '.' |
72 | registerBinding node, adapter, type, contexts[context], keypath | 72 | registerBinding node, adapter, type, contexts[context], keypath |
73 | |||
74 | if module? | ||
75 | module.exports = rivets | ||
76 | else | ||
77 | @rivets = rivets | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment