Build a queue of bindings and perform the binding routines after parsing. [Closes #3]
Showing
2 changed files
with
15 additions
and
16 deletions
... | @@ -35,7 +35,7 @@ | ... | @@ -35,7 +35,7 @@ |
35 | Binding.prototype.bind = function() { | 35 | Binding.prototype.bind = function() { |
36 | var _ref, | 36 | var _ref, |
37 | _this = this; | 37 | _this = this; |
38 | this.adapter.subscribe(this.context, this.keypath, function(value) { | 38 | this.set() && this.adapter.subscribe(this.context, this.keypath, function(value) { |
39 | return _this.set(value); | 39 | return _this.set(value); |
40 | }); | 40 | }); |
41 | if (_ref = this.type, __indexOf.call(Rivets.bidirectionals, _ref) >= 0) { | 41 | if (_ref = this.type, __indexOf.call(Rivets.bidirectionals, _ref) >= 0) { |
... | @@ -114,18 +114,15 @@ | ... | @@ -114,18 +114,15 @@ |
114 | return Rivets.bindings[routine] = routineFunction; | 114 | return Rivets.bindings[routine] = routineFunction; |
115 | }, | 115 | }, |
116 | bind: function(el, adapter, contexts) { | 116 | bind: function(el, adapter, contexts) { |
117 | var attribute, binding, context, keypath, node, path, type, _i, _len, _ref, _results; | 117 | var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; |
118 | if (contexts == null) { | 118 | if (contexts == null) { |
119 | contexts = {}; | 119 | contexts = {}; |
120 | } | 120 | } |
121 | bindings = []; | ||
121 | _ref = el.getElementsByTagName('*'); | 122 | _ref = el.getElementsByTagName('*'); |
122 | _results = []; | ||
123 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 123 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
124 | node = _ref[_i]; | 124 | node = _ref[_i]; |
125 | _results.push((function() { | ||
126 | var _j, _len1, _ref1, _results1; | ||
127 | _ref1 = node.attributes; | 125 | _ref1 = node.attributes; |
128 | _results1 = []; | ||
129 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 126 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
130 | attribute = _ref1[_j]; | 127 | attribute = _ref1[_j]; |
131 | if (/^data-/.test(attribute.name)) { | 128 | if (/^data-/.test(attribute.name)) { |
... | @@ -133,16 +130,15 @@ | ... | @@ -133,16 +130,15 @@ |
133 | path = attribute.value.split('.'); | 130 | path = attribute.value.split('.'); |
134 | context = path.shift(); | 131 | context = path.shift(); |
135 | keypath = path.join('.'); | 132 | keypath = path.join('.'); |
136 | binding = new Rivets.Binding(node, adapter, type, contexts[context], keypath); | 133 | bindings.push(new Rivets.Binding(node, adapter, type, contexts[context], keypath)); |
137 | _results1.push(binding.bind()); | 134 | } |
138 | } else { | ||
139 | _results1.push(void 0); | ||
140 | } | 135 | } |
141 | } | 136 | } |
142 | return _results1; | 137 | for (_k = 0, _len2 = bindings.length; _k < _len2; _k++) { |
143 | })()); | 138 | binding = bindings[_k]; |
139 | binding.bind(); | ||
144 | } | 140 | } |
145 | return _results; | 141 | return bindings.length; |
146 | } | 142 | } |
147 | }; | 143 | }; |
148 | 144 | ... | ... |
... | @@ -19,7 +19,7 @@ class Rivets.Binding | ... | @@ -19,7 +19,7 @@ class Rivets.Binding |
19 | # Conditionally also does the inverse and listens to the element for changes | 19 | # Conditionally also does the inverse and listens to the element for changes |
20 | # to propogate back to the context object. | 20 | # to propogate back to the context object. |
21 | bind: => | 21 | bind: => |
22 | @adapter.subscribe @context, @keypath, (value) => @set value | 22 | @set() and @adapter.subscribe @context, @keypath, (value) => @set value |
23 | 23 | ||
24 | if @type in Rivets.bidirectionals | 24 | if @type in Rivets.bidirectionals |
25 | @el.addEventListener 'change', (el) => | 25 | @el.addEventListener 'change', (el) => |
... | @@ -79,6 +79,8 @@ Rivets.interface = | ... | @@ -79,6 +79,8 @@ Rivets.interface = |
79 | Rivets.bindings[routine] = routineFunction | 79 | Rivets.bindings[routine] = routineFunction |
80 | 80 | ||
81 | bind: (el, adapter, contexts = {}) -> | 81 | bind: (el, adapter, contexts = {}) -> |
82 | bindings = [] | ||
83 | |||
82 | for node in el.getElementsByTagName '*' | 84 | for node in el.getElementsByTagName '*' |
83 | for attribute in node.attributes | 85 | for attribute in node.attributes |
84 | if /^data-/.test attribute.name | 86 | if /^data-/.test attribute.name |
... | @@ -86,9 +88,10 @@ Rivets.interface = | ... | @@ -86,9 +88,10 @@ Rivets.interface = |
86 | path = attribute.value.split '.' | 88 | path = attribute.value.split '.' |
87 | context = path.shift() | 89 | context = path.shift() |
88 | keypath = path.join '.' | 90 | keypath = path.join '.' |
91 | bindings.push new Rivets.Binding node, adapter, type, contexts[context], keypath | ||
89 | 92 | ||
90 | binding = new Rivets.Binding node, adapter, type, contexts[context], keypath | 93 | binding.bind() for binding in bindings |
91 | binding.bind() | 94 | bindings.length |
92 | 95 | ||
93 | # Exports rivets for both CommonJS and the browser. | 96 | # Exports rivets for both CommonJS and the browser. |
94 | if module? | 97 | if module? | ... | ... |
-
Please register or sign in to post a comment