Remove jQuery as a dependency.
Showing
2 changed files
with
48 additions
and
50 deletions
... | @@ -12,7 +12,7 @@ | ... | @@ -12,7 +12,7 @@ |
12 | return bind(el, value); | 12 | return bind(el, value); |
13 | }); | 13 | }); |
14 | if (__indexOf.call(bidirectionalBindings, type) >= 0) { | 14 | if (__indexOf.call(bidirectionalBindings, type) >= 0) { |
15 | return $(el).bind('change', function() { | 15 | return el.addEventListener('change', function() { |
16 | return adapter.publish(context, keypath, getInputValue(this)); | 16 | return adapter.publish(context, keypath, getInputValue(this)); |
17 | }); | 17 | }); |
18 | } | 18 | } |
... | @@ -22,36 +22,28 @@ | ... | @@ -22,36 +22,28 @@ |
22 | mirrored = false; | 22 | mirrored = false; |
23 | } | 23 | } |
24 | if (value) { | 24 | if (value) { |
25 | return $(el).attr(attr, mirrored ? attr : value); | 25 | return el.setAttribute(attr, mirrored ? attr : value); |
26 | } else { | 26 | } else { |
27 | return $(el).removeAttr(attr); | 27 | return el.removeAttribute(attr); |
28 | } | 28 | } |
29 | }; | 29 | }; |
30 | getInputValue = function(el) { | 30 | getInputValue = function(el) { |
31 | switch ($(el).attr('type')) { | 31 | switch (el.type) { |
32 | case 'text': | 32 | case 'text': |
33 | case 'textarea': | 33 | case 'textarea': |
34 | case 'password': | 34 | case 'password': |
35 | case 'select-one': | 35 | case 'select-one': |
36 | return $(el).val(); | 36 | return el.value; |
37 | case 'checkbox': | 37 | case 'checkbox': |
38 | return $(el).is(':checked'); | 38 | return el.checked; |
39 | } | 39 | } |
40 | }; | 40 | }; |
41 | bindings = { | 41 | bindings = { |
42 | show: function(el, value) { | 42 | show: function(el, value) { |
43 | if (value) { | 43 | return el.style.display = value ? '' : 'none'; |
44 | return $(el).show(); | ||
45 | } else { | ||
46 | return $(el).hide(); | ||
47 | } | ||
48 | }, | 44 | }, |
49 | hide: function(el, value) { | 45 | hide: function(el, value) { |
50 | if (value) { | 46 | return el.style.display = value ? 'none' : ''; |
51 | return $(el).hide(); | ||
52 | } else { | ||
53 | return $(el).show(); | ||
54 | } | ||
55 | }, | 47 | }, |
56 | enabled: function(el, value) { | 48 | enabled: function(el, value) { |
57 | return setAttribute(el, 'disabled', !value, true); | 49 | return setAttribute(el, 'disabled', !value, true); |
... | @@ -72,10 +64,10 @@ | ... | @@ -72,10 +64,10 @@ |
72 | return setAttribute(el, 'checked', !value, true); | 64 | return setAttribute(el, 'checked', !value, true); |
73 | }, | 65 | }, |
74 | text: function(el, value) { | 66 | text: function(el, value) { |
75 | return $(el).text(value || ''); | 67 | return el.innerHTML = value || ''; |
76 | }, | 68 | }, |
77 | value: function(el, value) { | 69 | value: function(el, value) { |
78 | return $(el).val(value); | 70 | return el.value = value; |
79 | } | 71 | } |
80 | }; | 72 | }; |
81 | attributeBinding = function(attr) { | 73 | attributeBinding = function(attr) { |
... | @@ -86,27 +78,32 @@ | ... | @@ -86,27 +78,32 @@ |
86 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']; | 78 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected']; |
87 | return { | 79 | return { |
88 | bind: function(el, adapter, contexts) { | 80 | bind: function(el, adapter, contexts) { |
81 | var nodes, _i, _ref, _results; | ||
89 | if (contexts == null) { | 82 | if (contexts == null) { |
90 | contexts = {}; | 83 | contexts = {}; |
91 | } | 84 | } |
92 | return $(el).add($('*', el)).each(function() { | 85 | nodes = el.getElementsByTagName('*'); |
93 | var nodeMap, target, _i, _ref, _results; | 86 | return (function() { |
94 | target = this; | 87 | _results = []; |
95 | nodeMap = target.attributes; | 88 | for (var _i = 0, _ref = nodes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); } |
96 | if (nodeMap.length > 0) { | 89 | return _results; |
90 | }).apply(this).forEach(function(n) { | ||
91 | var node, _i, _ref, _results; | ||
92 | node = nodes[n]; | ||
93 | if (node.attributes.length > 0) { | ||
97 | return (function() { | 94 | return (function() { |
98 | _results = []; | 95 | _results = []; |
99 | for (var _i = 0, _ref = nodeMap.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); } | 96 | for (var _i = 0, _ref = node.attributes.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); } |
100 | return _results; | 97 | return _results; |
101 | }).apply(this).forEach(function(n) { | 98 | }).apply(this).forEach(function(n) { |
102 | var context, keypath, node, path, type; | 99 | var attribute, context, keypath, path, type; |
103 | node = nodeMap[n]; | 100 | attribute = node.attributes[n]; |
104 | if (/^data-/.test(node.name)) { | 101 | if (/^data-/.test(attribute.name)) { |
105 | type = node.name.replace('data-', ''); | 102 | type = attribute.name.replace('data-', ''); |
106 | path = node.value.split('.'); | 103 | path = attribute.value.split('.'); |
107 | context = path.shift(); | 104 | context = path.shift(); |
108 | keypath = path.join('.'); | 105 | keypath = path.join('.'); |
109 | return registerBinding($(target), adapter, type, contexts[context], keypath); | 106 | return registerBinding(node, adapter, type, contexts[context], keypath); |
110 | } | 107 | } |
111 | }); | 108 | }); |
112 | } | 109 | } | ... | ... |
... | @@ -12,25 +12,25 @@ window.rivets = do -> | ... | @@ -12,25 +12,25 @@ window.rivets = do -> |
12 | bind el, value | 12 | bind el, value |
13 | 13 | ||
14 | if type in bidirectionalBindings | 14 | if type in bidirectionalBindings |
15 | $(el).bind 'change', -> | 15 | el.addEventListener 'change', -> |
16 | adapter.publish context, keypath, getInputValue this | 16 | adapter.publish context, keypath, getInputValue this |
17 | 17 | ||
18 | setAttribute = (el, attr, value, mirrored=false) -> | 18 | setAttribute = (el, attr, value, mirrored=false) -> |
19 | if value | 19 | if value |
20 | $(el).attr attr, if mirrored then attr else value | 20 | el.setAttribute attr, if mirrored then attr else value |
21 | else | 21 | else |
22 | $(el).removeAttr attr | 22 | el.removeAttribute attr |
23 | 23 | ||
24 | getInputValue = (el) -> | 24 | getInputValue = (el) -> |
25 | switch $(el).attr 'type' | 25 | switch el.type |
26 | when 'text', 'textarea', 'password', 'select-one' then $(el).val() | 26 | when 'text', 'textarea', 'password', 'select-one' then el.value |
27 | when 'checkbox' then $(el).is ':checked' | 27 | when 'checkbox' then el.checked |
28 | 28 | ||
29 | bindings = | 29 | bindings = |
30 | show: (el, value) -> | 30 | show: (el, value) -> |
31 | if value then $(el).show() else $(el).hide() | 31 | el.style.display = if value then '' else 'none' |
32 | hide: (el, value) -> | 32 | hide: (el, value) -> |
33 | if value then $(el).hide() else $(el).show() | 33 | el.style.display = if value then 'none' else '' |
34 | enabled: (el, value) -> | 34 | enabled: (el, value) -> |
35 | setAttribute el, 'disabled', !value, true | 35 | setAttribute el, 'disabled', !value, true |
36 | disabled: (el, value) -> | 36 | disabled: (el, value) -> |
... | @@ -44,9 +44,9 @@ window.rivets = do -> | ... | @@ -44,9 +44,9 @@ window.rivets = do -> |
44 | unselected: (el, value) -> | 44 | unselected: (el, value) -> |
45 | setAttribute el, 'checked', !value, true | 45 | setAttribute el, 'checked', !value, true |
46 | text: (el, value) -> | 46 | text: (el, value) -> |
47 | $(el).text value or '' | 47 | el.innerHTML = value or '' |
48 | value: (el, value) -> | 48 | value: (el, value) -> |
49 | $(el).val value | 49 | el.value = value |
50 | 50 | ||
51 | attributeBinding = (attr) -> | 51 | attributeBinding = (attr) -> |
52 | (el, value) -> | 52 | (el, value) -> |
... | @@ -55,17 +55,18 @@ window.rivets = do -> | ... | @@ -55,17 +55,18 @@ window.rivets = do -> |
55 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'] | 55 | bidirectionalBindings = ['value', 'checked', 'unchecked', 'selected', 'unselected'] |
56 | 56 | ||
57 | bind: (el, adapter, contexts={}) -> | 57 | bind: (el, adapter, contexts={}) -> |
58 | $(el).add($('*', el)).each -> | 58 | nodes = el.getElementsByTagName '*' |
59 | target = this | ||
60 | nodeMap = target.attributes | ||
61 | 59 | ||
62 | if nodeMap.length > 0 | 60 | [0..(nodes.length - 1)].forEach (n) -> |
63 | [0..(nodeMap.length - 1)].forEach (n) -> | 61 | node = nodes[n] |
64 | node = nodeMap[n] | 62 | |
63 | if node.attributes.length > 0 | ||
64 | [0..(node.attributes.length - 1)].forEach (n) -> | ||
65 | attribute = node.attributes[n] | ||
65 | 66 | ||
66 | if /^data-/.test node.name | 67 | if /^data-/.test attribute.name |
67 | type = node.name.replace 'data-', '' | 68 | type = attribute.name.replace 'data-', '' |
68 | path = node.value.split '.' | 69 | path = attribute.value.split '.' |
69 | context = path.shift() | 70 | context = path.shift() |
70 | keypath = path.join '.' | 71 | keypath = path.join '.' |
71 | registerBinding $(target), adapter, type, contexts[context], keypath | 72 | registerBinding node, adapter, type, contexts[context], keypath | ... | ... |
-
Please register or sign in to post a comment