Build 0.3.2.
Showing
5 changed files
with
106 additions
and
22 deletions
1 | # Rivets.js | 1 | # Rivets.js |
2 | 2 | ||
3 | Rivets.js is a declarative data binding facility that plays well with existing frameworks such as [Backbone.js](http://backbonejs.org), [Spine.js](http://spinejs.com) and [Stapes.js](http://hay.github.com/stapes/). It aims to be lightweight (1.9KB minified and gzipped), extensible, and configurable to work with any event-driven model. | 3 | Rivets.js is a declarative data binding facility that plays well with existing frameworks such as [Backbone.js](http://backbonejs.org), [Spine.js](http://spinejs.com) and [Stapes.js](http://hay.github.com/stapes/). It aims to be lightweight (2.3KB minified and gzipped), extensible, and configurable to work with any event-driven model. |
4 | 4 | ||
5 | --- | 5 | --- |
6 | 6 | ... | ... |
1 | // rivets.js | 1 | // rivets.js |
2 | // version: 0.3.1 | 2 | // version: 0.3.2 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, attributeBinding, bindEvent, eventBinding, getInputValue, rivets, unbindEvent, | 6 | var Rivets, attributeBinding, bindEvent, eventBinding, getInputValue, iterationBinding, rivets, unbindEvent, |
7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | 7 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, |
8 | __slice = [].slice, | 8 | __slice = [].slice, |
9 | __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; }; | 9 | __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; }; |
... | @@ -34,11 +34,16 @@ | ... | @@ -34,11 +34,16 @@ |
34 | 34 | ||
35 | this.formattedValue = __bind(this.formattedValue, this); | 35 | this.formattedValue = __bind(this.formattedValue, this); |
36 | 36 | ||
37 | if (this.options.special === "event") { | 37 | this.routine = (function() { |
38 | this.routine = eventBinding(this.type); | 38 | switch (this.options.special) { |
39 | } else { | 39 | case "event": |
40 | this.routine = Rivets.routines[this.type] || attributeBinding(this.type); | 40 | return eventBinding(this.type); |
41 | case "iteration": | ||
42 | return iterationBinding(this.type); | ||
43 | default: | ||
44 | return Rivets.routines[this.type] || attributeBinding(this.type); | ||
41 | } | 45 | } |
46 | }).call(this); | ||
42 | this.formatters = this.options.formatters || []; | 47 | this.formatters = this.options.formatters || []; |
43 | } | 48 | } |
44 | 49 | ||
... | @@ -61,9 +66,11 @@ | ... | @@ -61,9 +66,11 @@ |
61 | if (this.options.special === "event") { | 66 | if (this.options.special === "event") { |
62 | this.routine(this.el, value, this.currentListener); | 67 | this.routine(this.el, value, this.currentListener); |
63 | return this.currentListener = value; | 68 | return this.currentListener = value; |
69 | } else if (this.options.special === "iteration") { | ||
70 | return this.routine(this.el, value, this); | ||
64 | } else { | 71 | } else { |
65 | if (value instanceof Function) { | 72 | if (value instanceof Function) { |
66 | value = value(); | 73 | value = value.call(this.model); |
67 | } | 74 | } |
68 | return this.routine(this.el, value); | 75 | return this.routine(this.el, value); |
69 | } | 76 | } |
... | @@ -104,6 +111,7 @@ | ... | @@ -104,6 +111,7 @@ |
104 | 111 | ||
105 | Binding.prototype.unbind = function() { | 112 | Binding.prototype.unbind = function() { |
106 | var callback, keypath, _i, _len, _ref, _ref1, _ref2; | 113 | var callback, keypath, _i, _len, _ref, _ref1, _ref2; |
114 | if (!this.options.bypass) { | ||
107 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.set); | 115 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.set); |
108 | if ((_ref = this.options.dependencies) != null ? _ref.length : void 0) { | 116 | if ((_ref = this.options.dependencies) != null ? _ref.length : void 0) { |
109 | _ref1 = this.options.dependencies; | 117 | _ref1 = this.options.dependencies; |
... | @@ -116,6 +124,7 @@ | ... | @@ -116,6 +124,7 @@ |
116 | if (_ref2 = this.type, __indexOf.call(this.bidirectionals, _ref2) >= 0) { | 124 | if (_ref2 = this.type, __indexOf.call(this.bidirectionals, _ref2) >= 0) { |
117 | return this.el.removeEventListener('change', this.publish); | 125 | return this.el.removeEventListener('change', this.publish); |
118 | } | 126 | } |
127 | } | ||
119 | }; | 128 | }; |
120 | 129 | ||
121 | return Binding; | 130 | return Binding; |
... | @@ -152,36 +161,57 @@ | ... | @@ -152,36 +161,57 @@ |
152 | }; | 161 | }; |
153 | 162 | ||
154 | View.prototype.build = function() { | 163 | View.prototype.build = function() { |
155 | var bindingRegExp, el, eventRegExp, node, parseNode, _i, _len, _ref, _results, | 164 | var bindingRegExp, el, eventRegExp, iterationRegExp, iterator, node, parseNode, skipNodes, _i, _len, _ref, _results, |
156 | _this = this; | 165 | _this = this; |
157 | this.bindings = []; | 166 | this.bindings = []; |
167 | skipNodes = []; | ||
168 | iterator = null; | ||
158 | bindingRegExp = this.bindingRegExp(); | 169 | bindingRegExp = this.bindingRegExp(); |
159 | eventRegExp = /^on-/; | 170 | eventRegExp = /^on-/; |
171 | iterationRegExp = /^each-/; | ||
160 | parseNode = function(node) { | 172 | parseNode = function(node) { |
161 | var attribute, context, ctx, dependencies, keypath, model, options, path, pipe, pipes, splitPath, type, _i, _len, _ref, _results; | 173 | var a, attribute, binding, context, ctx, dependencies, keypath, model, n, options, path, pipe, pipes, splitPath, type, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _results; |
174 | if (__indexOf.call(skipNodes, node) < 0) { | ||
162 | _ref = node.attributes; | 175 | _ref = node.attributes; |
163 | _results = []; | ||
164 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 176 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
165 | attribute = _ref[_i]; | 177 | attribute = _ref[_i]; |
166 | if (bindingRegExp.test(attribute.name)) { | 178 | if (bindingRegExp.test(attribute.name)) { |
179 | type = attribute.name.replace(bindingRegExp, ''); | ||
180 | if (iterationRegExp.test(type)) { | ||
181 | if (!_this.models[type.replace(iterationRegExp, '')]) { | ||
182 | _ref1 = node.getElementsByTagName('*'); | ||
183 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
184 | n = _ref1[_j]; | ||
185 | skipNodes.push(n); | ||
186 | } | ||
187 | iterator = [attribute]; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | _ref2 = iterator || node.attributes; | ||
193 | _results = []; | ||
194 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | ||
195 | attribute = _ref2[_k]; | ||
196 | if (bindingRegExp.test(attribute.name)) { | ||
167 | options = {}; | 197 | options = {}; |
168 | type = attribute.name.replace(bindingRegExp, ''); | 198 | type = attribute.name.replace(bindingRegExp, ''); |
169 | pipes = (function() { | 199 | pipes = (function() { |
170 | var _j, _len1, _ref1, _results1; | 200 | var _l, _len3, _ref3, _results1; |
171 | _ref1 = attribute.value.split('|'); | 201 | _ref3 = attribute.value.split('|'); |
172 | _results1 = []; | 202 | _results1 = []; |
173 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 203 | for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { |
174 | pipe = _ref1[_j]; | 204 | pipe = _ref3[_l]; |
175 | _results1.push(pipe.trim()); | 205 | _results1.push(pipe.trim()); |
176 | } | 206 | } |
177 | return _results1; | 207 | return _results1; |
178 | })(); | 208 | })(); |
179 | context = (function() { | 209 | context = (function() { |
180 | var _j, _len1, _ref1, _results1; | 210 | var _l, _len3, _ref3, _results1; |
181 | _ref1 = pipes.shift().split('>'); | 211 | _ref3 = pipes.shift().split('>'); |
182 | _results1 = []; | 212 | _results1 = []; |
183 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | 213 | for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { |
184 | ctx = _ref1[_j]; | 214 | ctx = _ref3[_l]; |
185 | _results1.push(ctx.trim()); | 215 | _results1.push(ctx.trim()); |
186 | } | 216 | } |
187 | return _results1; | 217 | return _results1; |
... | @@ -192,6 +222,7 @@ | ... | @@ -192,6 +222,7 @@ |
192 | model = _this.models[splitPath.shift()]; | 222 | model = _this.models[splitPath.shift()]; |
193 | options.bypass = path.indexOf(":") !== -1; | 223 | options.bypass = path.indexOf(":") !== -1; |
194 | keypath = splitPath.join(); | 224 | keypath = splitPath.join(); |
225 | if (model) { | ||
195 | if (dependencies = context.shift()) { | 226 | if (dependencies = context.shift()) { |
196 | options.dependencies = dependencies.split(/\s+/); | 227 | options.dependencies = dependencies.split(/\s+/); |
197 | } | 228 | } |
... | @@ -199,12 +230,27 @@ | ... | @@ -199,12 +230,27 @@ |
199 | type = type.replace(eventRegExp, ''); | 230 | type = type.replace(eventRegExp, ''); |
200 | options.special = "event"; | 231 | options.special = "event"; |
201 | } | 232 | } |
202 | _results.push(_this.bindings.push(new Rivets.Binding(node, type, model, keypath, options))); | 233 | if (iterationRegExp.test(type)) { |
234 | type = type.replace(iterationRegExp, ''); | ||
235 | options.special = "iteration"; | ||
236 | } | ||
237 | binding = new Rivets.Binding(node, type, model, keypath, options); | ||
238 | binding.view = _this; | ||
239 | _this.bindings.push(binding); | ||
240 | } | ||
241 | } | ||
242 | if (iterator) { | ||
243 | for (_l = 0, _len3 = iterator.length; _l < _len3; _l++) { | ||
244 | a = iterator[_l]; | ||
245 | node.removeAttribute(a.name); | ||
246 | } | ||
247 | _results.push(iterator = null); | ||
203 | } else { | 248 | } else { |
204 | _results.push(void 0); | 249 | _results.push(void 0); |
205 | } | 250 | } |
206 | } | 251 | } |
207 | return _results; | 252 | return _results; |
253 | } | ||
208 | }; | 254 | }; |
209 | _ref = this.els; | 255 | _ref = this.els; |
210 | _results = []; | 256 | _results = []; |
... | @@ -289,6 +335,44 @@ | ... | @@ -289,6 +335,44 @@ |
289 | }; | 335 | }; |
290 | }; | 336 | }; |
291 | 337 | ||
338 | iterationBinding = function(name) { | ||
339 | return function(el, collection, binding) { | ||
340 | var data, item, itemEl, iteration, m, n, previous, _i, _j, _len, _len1, _ref, _ref1, _results; | ||
341 | if (binding.iterated != null) { | ||
342 | _ref = binding.iterated; | ||
343 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
344 | iteration = _ref[_i]; | ||
345 | iteration.view.unbind(); | ||
346 | iteration.el.parentNode.removeChild(iteration.el); | ||
347 | } | ||
348 | } else { | ||
349 | binding.marker = document.createComment(" rivets: each-" + name + " "); | ||
350 | el.parentNode.insertBefore(binding.marker, el); | ||
351 | el.parentNode.removeChild(el); | ||
352 | } | ||
353 | binding.iterated = []; | ||
354 | _results = []; | ||
355 | for (_j = 0, _len1 = collection.length; _j < _len1; _j++) { | ||
356 | item = collection[_j]; | ||
357 | data = {}; | ||
358 | _ref1 = binding.view.models; | ||
359 | for (n in _ref1) { | ||
360 | m = _ref1[n]; | ||
361 | data[n] = m; | ||
362 | } | ||
363 | data[name] = item; | ||
364 | itemEl = el.cloneNode(true); | ||
365 | previous = binding.iterated[binding.iterated.length - 1] || binding.marker; | ||
366 | binding.marker.parentNode.insertBefore(itemEl, previous.nextSibling); | ||
367 | _results.push(binding.iterated.push({ | ||
368 | el: itemEl, | ||
369 | view: rivets.bind(itemEl, data) | ||
370 | })); | ||
371 | } | ||
372 | return _results; | ||
373 | }; | ||
374 | }; | ||
375 | |||
292 | attributeBinding = function(attr) { | 376 | attributeBinding = function(attr) { |
293 | return function(el, value) { | 377 | return function(el, value) { |
294 | if (value) { | 378 | if (value) { | ... | ... |
This diff is collapsed.
Click to expand it.
1 | { | 1 | { |
2 | "name" : "rivets", | 2 | "name" : "rivets", |
3 | "description" : "Declarative data binding facility.", | 3 | "description" : "Declarative data binding facility.", |
4 | "version" : "0.3.1", | 4 | "version" : "0.3.2", |
5 | "author" : "Michael Richards", | 5 | "author" : "Michael Richards", |
6 | "url" : "http://rivetsjs.com", | 6 | "url" : "http://rivetsjs.com", |
7 | "main" : "./lib/rivets.js", | 7 | "main" : "./lib/rivets.js", | ... | ... |
-
Please register or sign in to post a comment