Build 0.6.6.
Showing
4 changed files
with
352 additions
and
322 deletions
... | @@ -2,7 +2,7 @@ | ... | @@ -2,7 +2,7 @@ |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "repo": "mikeric/rivets", | 3 | "repo": "mikeric/rivets", |
4 | "description": "Declarative data binding + templating solution.", | 4 | "description": "Declarative data binding + templating solution.", |
5 | "version": "0.6.5", | 5 | "version": "0.6.6", |
6 | "keywords": ["data binding", "templating"], | 6 | "keywords": ["data binding", "templating"], |
7 | "scripts": ["dist/rivets.js"], | 7 | "scripts": ["dist/rivets.js"], |
8 | "main": "dist/rivets.js", | 8 | "main": "dist/rivets.js", | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.6.5 | 2 | // version: 0.6.6 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, | 6 | var Rivets, bindMethod, unbindMethod, _ref, |
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 | __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; }, | 8 | __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 | __slice = [].slice, | 9 | __slice = [].slice, |
... | @@ -26,71 +26,70 @@ | ... | @@ -26,71 +26,70 @@ |
26 | } | 26 | } |
27 | }; | 27 | }; |
28 | 28 | ||
29 | Rivets.Util = { | 29 | if ('jQuery' in window) { |
30 | bindEvent: function(el, event, handler) { | 30 | _ref = 'on' in jQuery ? ['on', 'off'] : ['bind', 'unbind'], bindMethod = _ref[0], unbindMethod = _ref[1]; |
31 | if (window.jQuery != null) { | 31 | Rivets.Util = { |
32 | el = jQuery(el); | 32 | bindEvent: function(el, event, handler) { |
33 | if (el.on != null) { | 33 | return jQuery(el)[bindMethod](event, handler); |
34 | return el.on(event, handler); | 34 | }, |
35 | unbindEvent: function(el, event, handler) { | ||
36 | return jQuery(el)[unbindMethod](event, handler); | ||
37 | }, | ||
38 | getInputValue: function(el) { | ||
39 | var $el; | ||
40 | $el = jQuery(el); | ||
41 | if ($el.attr('type') === 'checkbox') { | ||
42 | return $el.is(':checked'); | ||
35 | } else { | 43 | } else { |
36 | return el.bind(event, handler); | 44 | return $el.val(); |
37 | } | 45 | } |
38 | } else if (window.addEventListener != null) { | ||
39 | return el.addEventListener(event, handler, false); | ||
40 | } else { | ||
41 | event = 'on' + event; | ||
42 | return el.attachEvent(event, handler); | ||
43 | } | 46 | } |
44 | }, | 47 | }; |
45 | unbindEvent: function(el, event, handler) { | 48 | } else { |
46 | if (window.jQuery != null) { | 49 | Rivets.Util = { |
47 | el = jQuery(el); | 50 | bindEvent: (function() { |
48 | if (el.off != null) { | 51 | if ('addEventListener' in window) { |
49 | return el.off(event, handler); | 52 | return function(el, event, handler) { |
50 | } else { | 53 | return el.addEventListener(event, handler, false); |
51 | return el.unbind(event, handler); | 54 | }; |
52 | } | 55 | } |
53 | } else if (window.removeEventListener != null) { | 56 | return function(el, event, handler) { |
54 | return el.removeEventListener(event, handler, false); | 57 | return el.attachEvent('on' + event, handler); |
55 | } else { | 58 | }; |
56 | event = 'on' + event; | 59 | })(), |
57 | return el.detachEvent(event, handler); | 60 | unbindEvent: (function() { |
58 | } | 61 | if ('removeEventListener' in window) { |
59 | }, | 62 | return function(el, event, handler) { |
60 | getInputValue: function(el) { | 63 | return el.removeEventListener(event, handler, false); |
61 | var o, _i, _len, _results; | 64 | }; |
62 | if (window.jQuery != null) { | ||
63 | el = jQuery(el); | ||
64 | switch (el[0].type) { | ||
65 | case 'checkbox': | ||
66 | return el.is(':checked'); | ||
67 | default: | ||
68 | return el.val(); | ||
69 | } | 65 | } |
70 | } else { | 66 | return function(el, event, handler) { |
71 | switch (el.type) { | 67 | return el.detachEvent('on' + event, handler); |
72 | case 'checkbox': | 68 | }; |
73 | return el.checked; | 69 | })(), |
74 | case 'select-multiple': | 70 | getInputValue: function(el) { |
75 | _results = []; | 71 | var o, _i, _len, _results; |
76 | for (_i = 0, _len = el.length; _i < _len; _i++) { | 72 | if (el.type === 'checkbox') { |
77 | o = el[_i]; | 73 | return el.checked; |
78 | if (o.selected) { | 74 | } else if (el.type === 'select-multiple') { |
79 | _results.push(o.value); | 75 | _results = []; |
80 | } | 76 | for (_i = 0, _len = el.length; _i < _len; _i++) { |
77 | o = el[_i]; | ||
78 | if (o.selected) { | ||
79 | _results.push(o.value); | ||
81 | } | 80 | } |
82 | return _results; | 81 | } |
83 | break; | 82 | return _results; |
84 | default: | 83 | } else { |
85 | return el.value; | 84 | return el.value; |
86 | } | 85 | } |
87 | } | 86 | } |
88 | } | 87 | }; |
89 | }; | 88 | } |
90 | 89 | ||
91 | Rivets.View = (function() { | 90 | Rivets.View = (function() { |
92 | function View(els, models, options) { | 91 | function View(els, models, options) { |
93 | var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; | 92 | var k, option, v, _base, _i, _len, _ref1, _ref2, _ref3; |
94 | this.els = els; | 93 | this.els = els; |
95 | this.models = models; | 94 | this.models = models; |
96 | this.options = options != null ? options : {}; | 95 | this.options = options != null ? options : {}; |
... | @@ -106,20 +105,20 @@ | ... | @@ -106,20 +105,20 @@ |
106 | if (!(this.els.jquery || this.els instanceof Array)) { | 105 | if (!(this.els.jquery || this.els instanceof Array)) { |
107 | this.els = [this.els]; | 106 | this.els = [this.els]; |
108 | } | 107 | } |
109 | _ref = ['config', 'binders', 'formatters', 'adapters']; | 108 | _ref1 = ['config', 'binders', 'formatters', 'adapters']; |
110 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 109 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
111 | option = _ref[_i]; | 110 | option = _ref1[_i]; |
112 | this[option] = {}; | 111 | this[option] = {}; |
113 | if (this.options[option]) { | 112 | if (this.options[option]) { |
114 | _ref1 = this.options[option]; | 113 | _ref2 = this.options[option]; |
115 | for (k in _ref1) { | 114 | for (k in _ref2) { |
116 | v = _ref1[k]; | 115 | v = _ref2[k]; |
117 | this[option][k] = v; | 116 | this[option][k] = v; |
118 | } | 117 | } |
119 | } | 118 | } |
120 | _ref2 = Rivets[option]; | 119 | _ref3 = Rivets[option]; |
121 | for (k in _ref2) { | 120 | for (k in _ref3) { |
122 | v = _ref2[k]; | 121 | v = _ref3[k]; |
123 | if ((_base = this[option])[k] == null) { | 122 | if ((_base = this[option])[k] == null) { |
124 | _base[k] = v; | 123 | _base[k] = v; |
125 | } | 124 | } |
... | @@ -137,7 +136,7 @@ | ... | @@ -137,7 +136,7 @@ |
137 | }; | 136 | }; |
138 | 137 | ||
139 | View.prototype.build = function() { | 138 | View.prototype.build = function() { |
140 | var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, | 139 | var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref1, |
141 | _this = this; | 140 | _this = this; |
142 | this.bindings = []; | 141 | this.bindings = []; |
143 | skipNodes = []; | 142 | skipNodes = []; |
... | @@ -147,21 +146,21 @@ | ... | @@ -147,21 +146,21 @@ |
147 | var context, ctx, dependencies, keypath, options, pipe, pipes; | 146 | var context, ctx, dependencies, keypath, options, pipe, pipes; |
148 | options = {}; | 147 | options = {}; |
149 | pipes = (function() { | 148 | pipes = (function() { |
150 | var _i, _len, _ref, _results; | 149 | var _i, _len, _ref1, _results; |
151 | _ref = declaration.split('|'); | 150 | _ref1 = declaration.split('|'); |
152 | _results = []; | 151 | _results = []; |
153 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 152 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
154 | pipe = _ref[_i]; | 153 | pipe = _ref1[_i]; |
155 | _results.push(pipe.trim()); | 154 | _results.push(pipe.trim()); |
156 | } | 155 | } |
157 | return _results; | 156 | return _results; |
158 | })(); | 157 | })(); |
159 | context = (function() { | 158 | context = (function() { |
160 | var _i, _len, _ref, _results; | 159 | var _i, _len, _ref1, _results; |
161 | _ref = pipes.shift().split('<'); | 160 | _ref1 = pipes.shift().split('<'); |
162 | _results = []; | 161 | _results = []; |
163 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 162 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
164 | ctx = _ref[_i]; | 163 | ctx = _ref1[_i]; |
165 | _results.push(ctx.trim()); | 164 | _results.push(ctx.trim()); |
166 | } | 165 | } |
167 | return _results; | 166 | return _results; |
... | @@ -174,7 +173,7 @@ | ... | @@ -174,7 +173,7 @@ |
174 | return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); | 173 | return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); |
175 | }; | 174 | }; |
176 | parse = function(node) { | 175 | parse = function(node) { |
177 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results; | 176 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3, _ref4, _ref5, _results; |
178 | if (__indexOf.call(skipNodes, node) < 0) { | 177 | if (__indexOf.call(skipNodes, node) < 0) { |
179 | if (node.nodeType === 3) { | 178 | if (node.nodeType === 3) { |
180 | parser = Rivets.TextTemplateParser; | 179 | parser = Rivets.TextTemplateParser; |
... | @@ -197,15 +196,15 @@ | ... | @@ -197,15 +196,15 @@ |
197 | type = node.tagName.replace(componentRegExp, '').toLowerCase(); | 196 | type = node.tagName.replace(componentRegExp, '').toLowerCase(); |
198 | _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); | 197 | _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); |
199 | } else if (node.attributes != null) { | 198 | } else if (node.attributes != null) { |
200 | _ref = node.attributes; | 199 | _ref1 = node.attributes; |
201 | for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | 200 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
202 | attribute = _ref[_j]; | 201 | attribute = _ref1[_j]; |
203 | if (bindingRegExp.test(attribute.name)) { | 202 | if (bindingRegExp.test(attribute.name)) { |
204 | type = attribute.name.replace(bindingRegExp, ''); | 203 | type = attribute.name.replace(bindingRegExp, ''); |
205 | if (!(binder = _this.binders[type])) { | 204 | if (!(binder = _this.binders[type])) { |
206 | _ref1 = _this.binders; | 205 | _ref2 = _this.binders; |
207 | for (identifier in _ref1) { | 206 | for (identifier in _ref2) { |
208 | value = _ref1[identifier]; | 207 | value = _ref2[identifier]; |
209 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | 208 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { |
210 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | 209 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); |
211 | if (regexp.test(type)) { | 210 | if (regexp.test(type)) { |
... | @@ -216,55 +215,55 @@ | ... | @@ -216,55 +215,55 @@ |
216 | } | 215 | } |
217 | binder || (binder = _this.binders['*']); | 216 | binder || (binder = _this.binders['*']); |
218 | if (binder.block) { | 217 | if (binder.block) { |
219 | _ref2 = node.childNodes; | 218 | _ref3 = node.childNodes; |
220 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | 219 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { |
221 | n = _ref2[_k]; | 220 | n = _ref3[_k]; |
222 | skipNodes.push(n); | 221 | skipNodes.push(n); |
223 | } | 222 | } |
224 | attributes = [attribute]; | 223 | attributes = [attribute]; |
225 | } | 224 | } |
226 | } | 225 | } |
227 | } | 226 | } |
228 | _ref3 = attributes || node.attributes; | 227 | _ref4 = attributes || node.attributes; |
229 | for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { | 228 | for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { |
230 | attribute = _ref3[_l]; | 229 | attribute = _ref4[_l]; |
231 | if (bindingRegExp.test(attribute.name)) { | 230 | if (bindingRegExp.test(attribute.name)) { |
232 | type = attribute.name.replace(bindingRegExp, ''); | 231 | type = attribute.name.replace(bindingRegExp, ''); |
233 | buildBinding('Binding', node, type, attribute.value); | 232 | buildBinding('Binding', node, type, attribute.value); |
234 | } | 233 | } |
235 | } | 234 | } |
236 | } | 235 | } |
237 | _ref4 = (function() { | 236 | _ref5 = (function() { |
238 | var _len4, _n, _ref4, _results1; | 237 | var _len4, _n, _ref5, _results1; |
239 | _ref4 = node.childNodes; | 238 | _ref5 = node.childNodes; |
240 | _results1 = []; | 239 | _results1 = []; |
241 | for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { | 240 | for (_n = 0, _len4 = _ref5.length; _n < _len4; _n++) { |
242 | n = _ref4[_n]; | 241 | n = _ref5[_n]; |
243 | _results1.push(n); | 242 | _results1.push(n); |
244 | } | 243 | } |
245 | return _results1; | 244 | return _results1; |
246 | })(); | 245 | })(); |
247 | _results = []; | 246 | _results = []; |
248 | for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) { | 247 | for (_m = 0, _len4 = _ref5.length; _m < _len4; _m++) { |
249 | childNode = _ref4[_m]; | 248 | childNode = _ref5[_m]; |
250 | _results.push(parse(childNode)); | 249 | _results.push(parse(childNode)); |
251 | } | 250 | } |
252 | return _results; | 251 | return _results; |
253 | } | 252 | } |
254 | }; | 253 | }; |
255 | _ref = this.els; | 254 | _ref1 = this.els; |
256 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 255 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
257 | el = _ref[_i]; | 256 | el = _ref1[_i]; |
258 | parse(el); | 257 | parse(el); |
259 | } | 258 | } |
260 | }; | 259 | }; |
261 | 260 | ||
262 | View.prototype.select = function(fn) { | 261 | View.prototype.select = function(fn) { |
263 | var binding, _i, _len, _ref, _results; | 262 | var binding, _i, _len, _ref1, _results; |
264 | _ref = this.bindings; | 263 | _ref1 = this.bindings; |
265 | _results = []; | 264 | _results = []; |
266 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 265 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
267 | binding = _ref[_i]; | 266 | binding = _ref1[_i]; |
268 | if (fn(binding)) { | 267 | if (fn(binding)) { |
269 | _results.push(binding); | 268 | _results.push(binding); |
270 | } | 269 | } |
... | @@ -273,53 +272,53 @@ | ... | @@ -273,53 +272,53 @@ |
273 | }; | 272 | }; |
274 | 273 | ||
275 | View.prototype.bind = function() { | 274 | View.prototype.bind = function() { |
276 | var binding, _i, _len, _ref, _results; | 275 | var binding, _i, _len, _ref1, _results; |
277 | _ref = this.bindings; | 276 | _ref1 = this.bindings; |
278 | _results = []; | 277 | _results = []; |
279 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 278 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
280 | binding = _ref[_i]; | 279 | binding = _ref1[_i]; |
281 | _results.push(binding.bind()); | 280 | _results.push(binding.bind()); |
282 | } | 281 | } |
283 | return _results; | 282 | return _results; |
284 | }; | 283 | }; |
285 | 284 | ||
286 | View.prototype.unbind = function() { | 285 | View.prototype.unbind = function() { |
287 | var binding, _i, _len, _ref, _results; | 286 | var binding, _i, _len, _ref1, _results; |
288 | _ref = this.bindings; | 287 | _ref1 = this.bindings; |
289 | _results = []; | 288 | _results = []; |
290 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 289 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
291 | binding = _ref[_i]; | 290 | binding = _ref1[_i]; |
292 | _results.push(binding.unbind()); | 291 | _results.push(binding.unbind()); |
293 | } | 292 | } |
294 | return _results; | 293 | return _results; |
295 | }; | 294 | }; |
296 | 295 | ||
297 | View.prototype.sync = function() { | 296 | View.prototype.sync = function() { |
298 | var binding, _i, _len, _ref, _results; | 297 | var binding, _i, _len, _ref1, _results; |
299 | _ref = this.bindings; | 298 | _ref1 = this.bindings; |
300 | _results = []; | 299 | _results = []; |
301 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 300 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
302 | binding = _ref[_i]; | 301 | binding = _ref1[_i]; |
303 | _results.push(binding.sync()); | 302 | _results.push(binding.sync()); |
304 | } | 303 | } |
305 | return _results; | 304 | return _results; |
306 | }; | 305 | }; |
307 | 306 | ||
308 | View.prototype.publish = function() { | 307 | View.prototype.publish = function() { |
309 | var binding, _i, _len, _ref, _results; | 308 | var binding, _i, _len, _ref1, _results; |
310 | _ref = this.select(function(b) { | 309 | _ref1 = this.select(function(b) { |
311 | return b.binder.publishes; | 310 | return b.binder.publishes; |
312 | }); | 311 | }); |
313 | _results = []; | 312 | _results = []; |
314 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 313 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
315 | binding = _ref[_i]; | 314 | binding = _ref1[_i]; |
316 | _results.push(binding.publish()); | 315 | _results.push(binding.publish()); |
317 | } | 316 | } |
318 | return _results; | 317 | return _results; |
319 | }; | 318 | }; |
320 | 319 | ||
321 | View.prototype.update = function(models) { | 320 | View.prototype.update = function(models) { |
322 | var binding, key, model, _i, _len, _ref, _results; | 321 | var binding, key, model, _i, _len, _ref1, _results; |
323 | if (models == null) { | 322 | if (models == null) { |
324 | models = {}; | 323 | models = {}; |
325 | } | 324 | } |
... | @@ -327,10 +326,10 @@ | ... | @@ -327,10 +326,10 @@ |
327 | model = models[key]; | 326 | model = models[key]; |
328 | this.models[key] = model; | 327 | this.models[key] = model; |
329 | } | 328 | } |
330 | _ref = this.bindings; | 329 | _ref1 = this.bindings; |
331 | _results = []; | 330 | _results = []; |
332 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 331 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
333 | binding = _ref[_i]; | 332 | binding = _ref1[_i]; |
334 | _results.push(binding.update(models)); | 333 | _results.push(binding.update(models)); |
335 | } | 334 | } |
336 | return _results; | 335 | return _results; |
... | @@ -355,20 +354,19 @@ | ... | @@ -355,20 +354,19 @@ |
355 | this.set = __bind(this.set, this); | 354 | this.set = __bind(this.set, this); |
356 | this.eventHandler = __bind(this.eventHandler, this); | 355 | this.eventHandler = __bind(this.eventHandler, this); |
357 | this.formattedValue = __bind(this.formattedValue, this); | 356 | this.formattedValue = __bind(this.formattedValue, this); |
358 | this.setObserver = __bind(this.setObserver, this); | ||
359 | this.setBinder = __bind(this.setBinder, this); | 357 | this.setBinder = __bind(this.setBinder, this); |
360 | this.formatters = this.options.formatters || []; | 358 | this.formatters = this.options.formatters || []; |
361 | this.dependencies = []; | 359 | this.dependencies = []; |
360 | this.model = void 0; | ||
362 | this.setBinder(); | 361 | this.setBinder(); |
363 | this.setObserver(); | ||
364 | } | 362 | } |
365 | 363 | ||
366 | Binding.prototype.setBinder = function() { | 364 | Binding.prototype.setBinder = function() { |
367 | var identifier, regexp, value, _ref; | 365 | var identifier, regexp, value, _ref1; |
368 | if (!(this.binder = this.view.binders[this.type])) { | 366 | if (!(this.binder = this.view.binders[this.type])) { |
369 | _ref = this.view.binders; | 367 | _ref1 = this.view.binders; |
370 | for (identifier in _ref) { | 368 | for (identifier in _ref1) { |
371 | value = _ref[identifier]; | 369 | value = _ref1[identifier]; |
372 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | 370 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { |
373 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | 371 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); |
374 | if (regexp.test(this.type)) { | 372 | if (regexp.test(this.type)) { |
... | @@ -387,27 +385,11 @@ | ... | @@ -387,27 +385,11 @@ |
387 | } | 385 | } |
388 | }; | 386 | }; |
389 | 387 | ||
390 | Binding.prototype.setObserver = function() { | ||
391 | var _this = this; | ||
392 | this.observer = new Rivets.KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { | ||
393 | if (_this.key) { | ||
394 | _this.unbind(true); | ||
395 | } | ||
396 | _this.model = obs.target; | ||
397 | if (_this.key) { | ||
398 | _this.bind(true); | ||
399 | } | ||
400 | return _this.sync(); | ||
401 | }); | ||
402 | this.key = this.observer.key; | ||
403 | return this.model = this.observer.target; | ||
404 | }; | ||
405 | |||
406 | Binding.prototype.formattedValue = function(value) { | 388 | Binding.prototype.formattedValue = function(value) { |
407 | var args, formatter, id, _i, _len, _ref; | 389 | var args, formatter, id, _i, _len, _ref1; |
408 | _ref = this.formatters; | 390 | _ref1 = this.formatters; |
409 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 391 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
410 | formatter = _ref[_i]; | 392 | formatter = _ref1[_i]; |
411 | args = formatter.split(/\s+/); | 393 | args = formatter.split(/\s+/); |
412 | id = args.shift(); | 394 | id = args.shift(); |
413 | formatter = this.view.formatters[id]; | 395 | formatter = this.view.formatters[id]; |
... | @@ -429,98 +411,87 @@ | ... | @@ -429,98 +411,87 @@ |
429 | }; | 411 | }; |
430 | 412 | ||
431 | Binding.prototype.set = function(value) { | 413 | Binding.prototype.set = function(value) { |
432 | var _ref; | 414 | var _ref1; |
433 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); | 415 | value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); |
434 | return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0; | 416 | return (_ref1 = this.binder.routine) != null ? _ref1.call(this, this.el, value) : void 0; |
435 | }; | 417 | }; |
436 | 418 | ||
437 | Binding.prototype.sync = function() { | 419 | Binding.prototype.sync = function() { |
438 | return this.set(this.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); | 420 | var dependency, observer, _i, _j, _len, _len1, _ref1, _ref2, _ref3; |
421 | if (this.model !== this.observer.target) { | ||
422 | _ref1 = this.dependencies; | ||
423 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
424 | observer = _ref1[_i]; | ||
425 | observer.unobserve(); | ||
426 | } | ||
427 | this.dependencies = []; | ||
428 | if (((this.model = this.observer.target) != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { | ||
429 | _ref3 = this.options.dependencies; | ||
430 | for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { | ||
431 | dependency = _ref3[_j]; | ||
432 | observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); | ||
433 | this.dependencies.push(observer); | ||
434 | } | ||
435 | } | ||
436 | } | ||
437 | return this.set(this.observer.value()); | ||
439 | }; | 438 | }; |
440 | 439 | ||
441 | Binding.prototype.publish = function() { | 440 | Binding.prototype.publish = function() { |
442 | var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2; | 441 | var args, formatter, id, value, _i, _len, _ref1, _ref2, _ref3; |
443 | value = Rivets.Util.getInputValue(this.el); | 442 | value = Rivets.Util.getInputValue(this.el); |
444 | _ref = this.formatters.slice(0).reverse(); | 443 | _ref1 = this.formatters.slice(0).reverse(); |
445 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 444 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
446 | formatter = _ref[_i]; | 445 | formatter = _ref1[_i]; |
447 | args = formatter.split(/\s+/); | 446 | args = formatter.split(/\s+/); |
448 | id = args.shift(); | 447 | id = args.shift(); |
449 | if ((_ref1 = this.view.formatters[id]) != null ? _ref1.publish : void 0) { | 448 | if ((_ref2 = this.view.formatters[id]) != null ? _ref2.publish : void 0) { |
450 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | 449 | value = (_ref3 = this.view.formatters[id]).publish.apply(_ref3, [value].concat(__slice.call(args))); |
451 | } | 450 | } |
452 | } | 451 | } |
453 | return this.view.adapters[this.key["interface"]].publish(this.model, this.key.path, value); | 452 | return this.observer.publish(value); |
454 | }; | 453 | }; |
455 | 454 | ||
456 | Binding.prototype.bind = function(silent) { | 455 | Binding.prototype.bind = function() { |
457 | var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, | 456 | var dependency, observer, _i, _len, _ref1, _ref2, _ref3; |
458 | _this = this; | 457 | if ((_ref1 = this.binder.bind) != null) { |
459 | if (silent == null) { | 458 | _ref1.call(this, this.el); |
460 | silent = false; | 459 | } |
461 | } | 460 | this.observer = new Rivets.Observer(this.view, this.view.models, this.keypath, this.sync); |
462 | if (!silent) { | 461 | this.model = this.observer.target; |
463 | if ((_ref = this.binder.bind) != null) { | 462 | if ((this.model != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { |
464 | _ref.call(this, this.el); | 463 | _ref3 = this.options.dependencies; |
464 | for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | ||
465 | dependency = _ref3[_i]; | ||
466 | observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); | ||
467 | this.dependencies.push(observer); | ||
465 | } | 468 | } |
466 | } | 469 | } |
467 | if (this.key) { | 470 | if (this.view.config.preloadData) { |
468 | this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); | 471 | return this.sync(); |
469 | } | ||
470 | if (!silent ? this.view.config.preloadData : void 0) { | ||
471 | this.sync(); | ||
472 | } | ||
473 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | ||
474 | _ref2 = this.options.dependencies; | ||
475 | _results = []; | ||
476 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
477 | dependency = _ref2[_i]; | ||
478 | observer = new Rivets.KeypathObserver(this.view, this.model, dependency, function(obs, prev) { | ||
479 | var key; | ||
480 | key = obs.key; | ||
481 | _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); | ||
482 | _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); | ||
483 | return _this.sync(); | ||
484 | }); | ||
485 | key = observer.key; | ||
486 | this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); | ||
487 | _results.push(this.dependencies.push(observer)); | ||
488 | } | ||
489 | return _results; | ||
490 | } | 472 | } |
491 | }; | 473 | }; |
492 | 474 | ||
493 | Binding.prototype.unbind = function(silent) { | 475 | Binding.prototype.unbind = function() { |
494 | var key, obs, _i, _len, _ref, _ref1; | 476 | var observer, _i, _len, _ref1, _ref2; |
495 | if (silent == null) { | 477 | if ((_ref1 = this.binder.unbind) != null) { |
496 | silent = false; | 478 | _ref1.call(this, this.el); |
497 | } | 479 | } |
498 | if (!silent) { | 480 | this.observer.unobserve(); |
499 | if ((_ref = this.binder.unbind) != null) { | 481 | _ref2 = this.dependencies; |
500 | _ref.call(this, this.el); | 482 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { |
501 | } | 483 | observer = _ref2[_i]; |
502 | this.observer.unobserve(); | 484 | observer.unobserve(); |
503 | } | ||
504 | if (this.key) { | ||
505 | this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); | ||
506 | } | ||
507 | if (this.dependencies.length) { | ||
508 | _ref1 = this.dependencies; | ||
509 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
510 | obs = _ref1[_i]; | ||
511 | key = obs.key; | ||
512 | this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); | ||
513 | } | ||
514 | return this.dependencies = []; | ||
515 | } | 485 | } |
486 | return this.dependencies = []; | ||
516 | }; | 487 | }; |
517 | 488 | ||
518 | Binding.prototype.update = function(models) { | 489 | Binding.prototype.update = function(models) { |
519 | var _ref; | 490 | var _ref1; |
520 | if (models == null) { | 491 | if (models == null) { |
521 | models = {}; | 492 | models = {}; |
522 | } | 493 | } |
523 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; | 494 | return (_ref1 = this.binder.update) != null ? _ref1.call(this, models) : void 0; |
524 | }; | 495 | }; |
525 | 496 | ||
526 | return Binding; | 497 | return Binding; |
... | @@ -531,7 +502,7 @@ | ... | @@ -531,7 +502,7 @@ |
531 | __extends(ComponentBinding, _super); | 502 | __extends(ComponentBinding, _super); |
532 | 503 | ||
533 | function ComponentBinding(view, el, type) { | 504 | function ComponentBinding(view, el, type) { |
534 | var attribute, _i, _len, _ref, _ref1; | 505 | var attribute, _i, _len, _ref1, _ref2; |
535 | this.view = view; | 506 | this.view = view; |
536 | this.el = el; | 507 | this.el = el; |
537 | this.type = type; | 508 | this.type = type; |
... | @@ -542,10 +513,10 @@ | ... | @@ -542,10 +513,10 @@ |
542 | this.component = Rivets.components[this.type]; | 513 | this.component = Rivets.components[this.type]; |
543 | this.attributes = {}; | 514 | this.attributes = {}; |
544 | this.inflections = {}; | 515 | this.inflections = {}; |
545 | _ref = this.el.attributes || []; | 516 | _ref1 = this.el.attributes || []; |
546 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 517 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
547 | attribute = _ref[_i]; | 518 | attribute = _ref1[_i]; |
548 | if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { | 519 | if (_ref2 = attribute.name, __indexOf.call(this.component.attributes, _ref2) >= 0) { |
549 | this.attributes[attribute.name] = attribute.value; | 520 | this.attributes[attribute.name] = attribute.value; |
550 | } else { | 521 | } else { |
551 | this.inflections[attribute.name] = attribute.value; | 522 | this.inflections[attribute.name] = attribute.value; |
... | @@ -556,17 +527,17 @@ | ... | @@ -556,17 +527,17 @@ |
556 | ComponentBinding.prototype.sync = function() {}; | 527 | ComponentBinding.prototype.sync = function() {}; |
557 | 528 | ||
558 | ComponentBinding.prototype.locals = function(models) { | 529 | ComponentBinding.prototype.locals = function(models) { |
559 | var inverse, key, model, path, result, _i, _len, _ref, _ref1; | 530 | var inverse, key, model, path, result, _i, _len, _ref1, _ref2; |
560 | if (models == null) { | 531 | if (models == null) { |
561 | models = this.view.models; | 532 | models = this.view.models; |
562 | } | 533 | } |
563 | result = {}; | 534 | result = {}; |
564 | _ref = this.inflections; | 535 | _ref1 = this.inflections; |
565 | for (key in _ref) { | 536 | for (key in _ref1) { |
566 | inverse = _ref[key]; | 537 | inverse = _ref1[key]; |
567 | _ref1 = inverse.split('.'); | 538 | _ref2 = inverse.split('.'); |
568 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | 539 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { |
569 | path = _ref1[_i]; | 540 | path = _ref2[_i]; |
570 | result[key] = (result[key] || models)[path]; | 541 | result[key] = (result[key] || models)[path]; |
571 | } | 542 | } |
572 | } | 543 | } |
... | @@ -580,14 +551,14 @@ | ... | @@ -580,14 +551,14 @@ |
580 | }; | 551 | }; |
581 | 552 | ||
582 | ComponentBinding.prototype.update = function(models) { | 553 | ComponentBinding.prototype.update = function(models) { |
583 | var _ref; | 554 | var _ref1; |
584 | return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; | 555 | return (_ref1 = this.componentView) != null ? _ref1.update(this.locals(models)) : void 0; |
585 | }; | 556 | }; |
586 | 557 | ||
587 | ComponentBinding.prototype.bind = function() { | 558 | ComponentBinding.prototype.bind = function() { |
588 | var el, _ref; | 559 | var el, _ref1; |
589 | if (this.componentView != null) { | 560 | if (this.componentView != null) { |
590 | return (_ref = this.componentView) != null ? _ref.bind() : void 0; | 561 | return (_ref1 = this.componentView) != null ? _ref1.bind() : void 0; |
591 | } else { | 562 | } else { |
592 | el = this.component.build.call(this.attributes); | 563 | el = this.component.build.call(this.attributes); |
593 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); | 564 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); |
... | @@ -596,8 +567,8 @@ | ... | @@ -596,8 +567,8 @@ |
596 | }; | 567 | }; |
597 | 568 | ||
598 | ComponentBinding.prototype.unbind = function() { | 569 | ComponentBinding.prototype.unbind = function() { |
599 | var _ref; | 570 | var _ref1; |
600 | return (_ref = this.componentView) != null ? _ref.unbind() : void 0; | 571 | return (_ref1 = this.componentView) != null ? _ref1.unbind() : void 0; |
601 | }; | 572 | }; |
602 | 573 | ||
603 | return ComponentBinding; | 574 | return ComponentBinding; |
... | @@ -616,7 +587,6 @@ | ... | @@ -616,7 +587,6 @@ |
616 | this.sync = __bind(this.sync, this); | 587 | this.sync = __bind(this.sync, this); |
617 | this.formatters = this.options.formatters || []; | 588 | this.formatters = this.options.formatters || []; |
618 | this.dependencies = []; | 589 | this.dependencies = []; |
619 | this.setObserver(); | ||
620 | } | 590 | } |
621 | 591 | ||
622 | TextBinding.prototype.binder = { | 592 | TextBinding.prototype.binder = { |
... | @@ -637,14 +607,14 @@ | ... | @@ -637,14 +607,14 @@ |
637 | function KeypathParser() {} | 607 | function KeypathParser() {} |
638 | 608 | ||
639 | KeypathParser.parse = function(keypath, interfaces, root) { | 609 | KeypathParser.parse = function(keypath, interfaces, root) { |
640 | var char, current, tokens, _i, _len; | 610 | var char, current, index, tokens, _i, _ref1; |
641 | tokens = []; | 611 | tokens = []; |
642 | current = { | 612 | current = { |
643 | "interface": root, | 613 | "interface": root, |
644 | path: '' | 614 | path: '' |
645 | }; | 615 | }; |
646 | for (_i = 0, _len = keypath.length; _i < _len; _i++) { | 616 | for (index = _i = 0, _ref1 = keypath.length; _i < _ref1; index = _i += 1) { |
647 | char = keypath[_i]; | 617 | char = keypath.charAt(index); |
648 | if (__indexOf.call(interfaces, char) >= 0) { | 618 | if (__indexOf.call(interfaces, char) >= 0) { |
649 | tokens.push(current); | 619 | tokens.push(current); |
650 | current = { | 620 | current = { |
... | @@ -722,34 +692,39 @@ | ... | @@ -722,34 +692,39 @@ |
722 | 692 | ||
723 | })(); | 693 | })(); |
724 | 694 | ||
725 | Rivets.KeypathObserver = (function() { | 695 | Rivets.Observer = (function() { |
726 | function KeypathObserver(view, model, keypath, callback) { | 696 | function Observer(view, model, keypath, callback) { |
727 | this.view = view; | 697 | this.view = view; |
728 | this.model = model; | 698 | this.model = model; |
729 | this.keypath = keypath; | 699 | this.keypath = keypath; |
730 | this.callback = callback; | 700 | this.callback = callback; |
731 | this.unobserve = __bind(this.unobserve, this); | 701 | this.unobserve = __bind(this.unobserve, this); |
732 | this.realize = __bind(this.realize, this); | 702 | this.realize = __bind(this.realize, this); |
703 | this.value = __bind(this.value, this); | ||
704 | this.publish = __bind(this.publish, this); | ||
705 | this.read = __bind(this.read, this); | ||
706 | this.set = __bind(this.set, this); | ||
707 | this.adapter = __bind(this.adapter, this); | ||
733 | this.update = __bind(this.update, this); | 708 | this.update = __bind(this.update, this); |
709 | this.initialize = __bind(this.initialize, this); | ||
734 | this.parse = __bind(this.parse, this); | 710 | this.parse = __bind(this.parse, this); |
735 | this.parse(); | 711 | this.parse(); |
736 | this.objectPath = []; | 712 | this.initialize(); |
737 | this.target = this.realize(); | ||
738 | } | 713 | } |
739 | 714 | ||
740 | KeypathObserver.prototype.parse = function() { | 715 | Observer.prototype.parse = function() { |
741 | var interfaces, k, path, root, v, _ref; | 716 | var interfaces, k, path, root, v, _ref1; |
742 | interfaces = (function() { | 717 | interfaces = (function() { |
743 | var _ref, _results; | 718 | var _ref1, _results; |
744 | _ref = this.view.adapters; | 719 | _ref1 = this.view.adapters; |
745 | _results = []; | 720 | _results = []; |
746 | for (k in _ref) { | 721 | for (k in _ref1) { |
747 | v = _ref[k]; | 722 | v = _ref1[k]; |
748 | _results.push(k); | 723 | _results.push(k); |
749 | } | 724 | } |
750 | return _results; | 725 | return _results; |
751 | }).call(this); | 726 | }).call(this); |
752 | if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { | 727 | if (_ref1 = this.keypath[0], __indexOf.call(interfaces, _ref1) >= 0) { |
753 | root = this.keypath[0]; | 728 | root = this.keypath[0]; |
754 | path = this.keypath.substr(1); | 729 | path = this.keypath.substr(1); |
755 | } else { | 730 | } else { |
... | @@ -760,44 +735,99 @@ | ... | @@ -760,44 +735,99 @@ |
760 | return this.key = this.tokens.pop(); | 735 | return this.key = this.tokens.pop(); |
761 | }; | 736 | }; |
762 | 737 | ||
763 | KeypathObserver.prototype.update = function() { | 738 | Observer.prototype.initialize = function() { |
764 | var next, prev; | 739 | this.objectPath = []; |
740 | this.target = this.realize(); | ||
741 | if (this.target != null) { | ||
742 | return this.set(true, this.key, this.target, this.callback); | ||
743 | } | ||
744 | }; | ||
745 | |||
746 | Observer.prototype.update = function() { | ||
747 | var next, oldValue; | ||
765 | if ((next = this.realize()) !== this.target) { | 748 | if ((next = this.realize()) !== this.target) { |
766 | prev = this.target; | 749 | if (this.target != null) { |
750 | this.set(false, this.key, this.target, this.callback); | ||
751 | } | ||
752 | if (next != null) { | ||
753 | this.set(true, this.key, next, this.callback); | ||
754 | } | ||
755 | oldValue = this.value(); | ||
767 | this.target = next; | 756 | this.target = next; |
768 | return this.callback(this, prev); | 757 | if (this.value() !== oldValue) { |
758 | return this.callback(); | ||
759 | } | ||
769 | } | 760 | } |
770 | }; | 761 | }; |
771 | 762 | ||
772 | KeypathObserver.prototype.realize = function() { | 763 | Observer.prototype.adapter = function(key) { |
773 | var current, index, prev, token, _i, _len, _ref; | 764 | return this.view.adapters[key["interface"]]; |
765 | }; | ||
766 | |||
767 | Observer.prototype.set = function(active, key, obj, callback) { | ||
768 | var action; | ||
769 | action = active ? 'subscribe' : 'unsubscribe'; | ||
770 | return this.adapter(key)[action](obj, key.path, callback); | ||
771 | }; | ||
772 | |||
773 | Observer.prototype.read = function(key, obj) { | ||
774 | return this.adapter(key).read(obj, key.path); | ||
775 | }; | ||
776 | |||
777 | Observer.prototype.publish = function(value) { | ||
778 | if (this.target != null) { | ||
779 | return this.adapter(this.key).publish(this.target, this.key.path, value); | ||
780 | } | ||
781 | }; | ||
782 | |||
783 | Observer.prototype.value = function() { | ||
784 | if (this.target != null) { | ||
785 | return this.read(this.key, this.target); | ||
786 | } | ||
787 | }; | ||
788 | |||
789 | Observer.prototype.realize = function() { | ||
790 | var current, index, prev, token, unreached, _i, _len, _ref1; | ||
774 | current = this.model; | 791 | current = this.model; |
775 | _ref = this.tokens; | 792 | unreached = null; |
776 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | 793 | _ref1 = this.tokens; |
777 | token = _ref[index]; | 794 | for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { |
778 | if (this.objectPath[index] != null) { | 795 | token = _ref1[index]; |
779 | if (current !== (prev = this.objectPath[index])) { | 796 | if (current != null) { |
780 | this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); | 797 | if (this.objectPath[index] != null) { |
781 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); | 798 | if (current !== (prev = this.objectPath[index])) { |
799 | this.set(false, token, prev, this.update); | ||
800 | this.set(true, token, current, this.update); | ||
801 | this.objectPath[index] = current; | ||
802 | } | ||
803 | } else { | ||
804 | this.set(true, token, current, this.update); | ||
782 | this.objectPath[index] = current; | 805 | this.objectPath[index] = current; |
783 | } | 806 | } |
807 | current = this.read(token, current); | ||
784 | } else { | 808 | } else { |
785 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); | 809 | if (unreached == null) { |
786 | this.objectPath[index] = current; | 810 | unreached = index; |
811 | } | ||
812 | if (prev = this.objectPath[index]) { | ||
813 | this.set(false, token, prev, this.update); | ||
814 | } | ||
787 | } | 815 | } |
788 | current = this.view.adapters[token["interface"]].read(current, token.path); | 816 | } |
817 | if (unreached != null) { | ||
818 | this.objectPath.splice(unreached); | ||
789 | } | 819 | } |
790 | return current; | 820 | return current; |
791 | }; | 821 | }; |
792 | 822 | ||
793 | KeypathObserver.prototype.unobserve = function() { | 823 | Observer.prototype.unobserve = function() { |
794 | var index, obj, token, _i, _len, _ref, _results; | 824 | var index, obj, token, _i, _len, _ref1, _results; |
795 | _ref = this.tokens; | 825 | _ref1 = this.tokens; |
796 | _results = []; | 826 | _results = []; |
797 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | 827 | for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { |
798 | token = _ref[index]; | 828 | token = _ref1[index]; |
799 | if (obj = this.objectPath[index]) { | 829 | if (obj = this.objectPath[index]) { |
800 | _results.push(this.view.adapters[token["interface"]].unsubscribe(obj, token.path, this.update)); | 830 | _results.push(this.set(false, token, obj, this.update)); |
801 | } else { | 831 | } else { |
802 | _results.push(void 0); | 832 | _results.push(void 0); |
803 | } | 833 | } |
... | @@ -805,7 +835,7 @@ | ... | @@ -805,7 +835,7 @@ |
805 | return _results; | 835 | return _results; |
806 | }; | 836 | }; |
807 | 837 | ||
808 | return KeypathObserver; | 838 | return Observer; |
809 | 839 | ||
810 | })(); | 840 | })(); |
811 | 841 | ||
... | @@ -846,9 +876,9 @@ | ... | @@ -846,9 +876,9 @@ |
846 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 876 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
847 | }, | 877 | }, |
848 | routine: function(el, value) { | 878 | routine: function(el, value) { |
849 | var _ref; | 879 | var _ref1; |
850 | if (el.type === 'radio') { | 880 | if (el.type === 'radio') { |
851 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0); | 881 | return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) === (value != null ? value.toString() : void 0); |
852 | } else { | 882 | } else { |
853 | return el.checked = !!value; | 883 | return el.checked = !!value; |
854 | } | 884 | } |
... | @@ -864,9 +894,9 @@ | ... | @@ -864,9 +894,9 @@ |
864 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 894 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
865 | }, | 895 | }, |
866 | routine: function(el, value) { | 896 | routine: function(el, value) { |
867 | var _ref; | 897 | var _ref1; |
868 | if (el.type === 'radio') { | 898 | if (el.type === 'radio') { |
869 | return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0); | 899 | return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) !== (value != null ? value.toString() : void 0); |
870 | } else { | 900 | } else { |
871 | return el.checked = !value; | 901 | return el.checked = !value; |
872 | } | 902 | } |
... | @@ -882,10 +912,10 @@ | ... | @@ -882,10 +912,10 @@ |
882 | return Rivets.Util.unbindEvent(el, 'change', this.publish); | 912 | return Rivets.Util.unbindEvent(el, 'change', this.publish); |
883 | }, | 913 | }, |
884 | routine: function(el, value) { | 914 | routine: function(el, value) { |
885 | var o, _i, _len, _ref, _ref1, _ref2, _results; | 915 | var o, _i, _len, _ref1, _ref2, _ref3, _results; |
886 | if (window.jQuery != null) { | 916 | if (window.jQuery != null) { |
887 | el = jQuery(el); | 917 | el = jQuery(el); |
888 | if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) { | 918 | if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.val()) != null ? _ref1.toString() : void 0)) { |
889 | return el.val(value != null ? value : ''); | 919 | return el.val(value != null ? value : ''); |
890 | } | 920 | } |
891 | } else { | 921 | } else { |
... | @@ -894,11 +924,11 @@ | ... | @@ -894,11 +924,11 @@ |
894 | _results = []; | 924 | _results = []; |
895 | for (_i = 0, _len = el.length; _i < _len; _i++) { | 925 | for (_i = 0, _len = el.length; _i < _len; _i++) { |
896 | o = el[_i]; | 926 | o = el[_i]; |
897 | _results.push(o.selected = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0)); | 927 | _results.push(o.selected = (_ref2 = o.value, __indexOf.call(value, _ref2) >= 0)); |
898 | } | 928 | } |
899 | return _results; | 929 | return _results; |
900 | } | 930 | } |
901 | } else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.toString() : void 0)) { | 931 | } else if ((value != null ? value.toString() : void 0) !== ((_ref3 = el.value) != null ? _ref3.toString() : void 0)) { |
902 | return el.value = value != null ? value : ''; | 932 | return el.value = value != null ? value : ''; |
903 | } | 933 | } |
904 | } | 934 | } |
... | @@ -919,17 +949,17 @@ | ... | @@ -919,17 +949,17 @@ |
919 | } | 949 | } |
920 | }, | 950 | }, |
921 | unbind: function() { | 951 | unbind: function() { |
922 | var _ref; | 952 | var _ref1; |
923 | return (_ref = this.nested) != null ? _ref.unbind() : void 0; | 953 | return (_ref1 = this.nested) != null ? _ref1.unbind() : void 0; |
924 | }, | 954 | }, |
925 | routine: function(el, value) { | 955 | routine: function(el, value) { |
926 | var key, model, models, options, _ref; | 956 | var key, model, models, options, _ref1; |
927 | if (!!value === (this.nested == null)) { | 957 | if (!!value === (this.nested == null)) { |
928 | if (value) { | 958 | if (value) { |
929 | models = {}; | 959 | models = {}; |
930 | _ref = this.view.models; | 960 | _ref1 = this.view.models; |
931 | for (key in _ref) { | 961 | for (key in _ref1) { |
932 | model = _ref[key]; | 962 | model = _ref1[key]; |
933 | models[key] = model; | 963 | models[key] = model; |
934 | } | 964 | } |
935 | options = { | 965 | options = { |
... | @@ -948,8 +978,8 @@ | ... | @@ -948,8 +978,8 @@ |
948 | } | 978 | } |
949 | }, | 979 | }, |
950 | update: function(models) { | 980 | update: function(models) { |
951 | var _ref; | 981 | var _ref1; |
952 | return (_ref = this.nested) != null ? _ref.update(models) : void 0; | 982 | return (_ref1 = this.nested) != null ? _ref1.update(models) : void 0; |
953 | } | 983 | } |
954 | }; | 984 | }; |
955 | 985 | ||
... | @@ -998,25 +1028,25 @@ | ... | @@ -998,25 +1028,25 @@ |
998 | } | 1028 | } |
999 | }, | 1029 | }, |
1000 | unbind: function(el) { | 1030 | unbind: function(el) { |
1001 | var view, _i, _len, _ref, _results; | 1031 | var view, _i, _len, _ref1, _results; |
1002 | if (this.iterated != null) { | 1032 | if (this.iterated != null) { |
1003 | _ref = this.iterated; | 1033 | _ref1 = this.iterated; |
1004 | _results = []; | 1034 | _results = []; |
1005 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 1035 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
1006 | view = _ref[_i]; | 1036 | view = _ref1[_i]; |
1007 | _results.push(view.unbind()); | 1037 | _results.push(view.unbind()); |
1008 | } | 1038 | } |
1009 | return _results; | 1039 | return _results; |
1010 | } | 1040 | } |
1011 | }, | 1041 | }, |
1012 | routine: function(el, collection) { | 1042 | routine: function(el, collection) { |
1013 | var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results; | 1043 | var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results; |
1014 | modelName = this.args[0]; | 1044 | modelName = this.args[0]; |
1015 | collection = collection || []; | 1045 | collection = collection || []; |
1016 | if (this.iterated.length > collection.length) { | 1046 | if (this.iterated.length > collection.length) { |
1017 | _ref = Array(this.iterated.length - collection.length); | 1047 | _ref1 = Array(this.iterated.length - collection.length); |
1018 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 1048 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
1019 | i = _ref[_i]; | 1049 | i = _ref1[_i]; |
1020 | view = this.iterated.pop(); | 1050 | view = this.iterated.pop(); |
1021 | view.unbind(); | 1051 | view.unbind(); |
1022 | this.marker.parentNode.removeChild(view.els[0]); | 1052 | this.marker.parentNode.removeChild(view.els[0]); |
... | @@ -1027,9 +1057,9 @@ | ... | @@ -1027,9 +1057,9 @@ |
1027 | data = {}; | 1057 | data = {}; |
1028 | data[modelName] = model; | 1058 | data[modelName] = model; |
1029 | if (this.iterated[index] == null) { | 1059 | if (this.iterated[index] == null) { |
1030 | _ref1 = this.view.models; | 1060 | _ref2 = this.view.models; |
1031 | for (key in _ref1) { | 1061 | for (key in _ref2) { |
1032 | model = _ref1[key]; | 1062 | model = _ref2[key]; |
1033 | if (data[key] == null) { | 1063 | if (data[key] == null) { |
1034 | data[key] = model; | 1064 | data[key] = model; |
1035 | } | 1065 | } |
... | @@ -1041,9 +1071,9 @@ | ... | @@ -1041,9 +1071,9 @@ |
1041 | adapters: this.view.options.adapters, | 1071 | adapters: this.view.options.adapters, |
1042 | config: {} | 1072 | config: {} |
1043 | }; | 1073 | }; |
1044 | _ref2 = this.view.options.config; | 1074 | _ref3 = this.view.options.config; |
1045 | for (k in _ref2) { | 1075 | for (k in _ref3) { |
1046 | v = _ref2[k]; | 1076 | v = _ref3[k]; |
1047 | options.config[k] = v; | 1077 | options.config[k] = v; |
1048 | } | 1078 | } |
1049 | options.config.preloadData = true; | 1079 | options.config.preloadData = true; |
... | @@ -1057,10 +1087,10 @@ | ... | @@ -1057,10 +1087,10 @@ |
1057 | } | 1087 | } |
1058 | } | 1088 | } |
1059 | if (el.nodeName === 'OPTION') { | 1089 | if (el.nodeName === 'OPTION') { |
1060 | _ref3 = this.view.bindings; | 1090 | _ref4 = this.view.bindings; |
1061 | _results = []; | 1091 | _results = []; |
1062 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { | 1092 | for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) { |
1063 | binding = _ref3[_k]; | 1093 | binding = _ref4[_k]; |
1064 | if (binding.el === this.marker.parentNode && binding.type === 'value') { | 1094 | if (binding.el === this.marker.parentNode && binding.type === 'value') { |
1065 | _results.push(binding.sync()); | 1095 | _results.push(binding.sync()); |
1066 | } else { | 1096 | } else { |
... | @@ -1071,7 +1101,7 @@ | ... | @@ -1071,7 +1101,7 @@ |
1071 | } | 1101 | } |
1072 | }, | 1102 | }, |
1073 | update: function(models) { | 1103 | update: function(models) { |
1074 | var data, key, model, view, _i, _len, _ref, _results; | 1104 | var data, key, model, view, _i, _len, _ref1, _results; |
1075 | data = {}; | 1105 | data = {}; |
1076 | for (key in models) { | 1106 | for (key in models) { |
1077 | model = models[key]; | 1107 | model = models[key]; |
... | @@ -1079,10 +1109,10 @@ | ... | @@ -1079,10 +1109,10 @@ |
1079 | data[key] = model; | 1109 | data[key] = model; |
1080 | } | 1110 | } |
1081 | } | 1111 | } |
1082 | _ref = this.iterated; | 1112 | _ref1 = this.iterated; |
1083 | _results = []; | 1113 | _results = []; |
1084 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 1114 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
1085 | view = _ref[_i]; | 1115 | view = _ref1[_i]; |
1086 | _results.push(view.update(data)); | 1116 | _results.push(view.update(data)); |
1087 | } | 1117 | } |
1088 | return _results; | 1118 | return _results; |
... | @@ -1128,14 +1158,14 @@ | ... | @@ -1128,14 +1158,14 @@ |
1128 | map = this.weakReference(obj); | 1158 | map = this.weakReference(obj); |
1129 | weakmap = this.weakmap; | 1159 | weakmap = this.weakmap; |
1130 | return obj[fn] = function() { | 1160 | return obj[fn] = function() { |
1131 | var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; | 1161 | var callback, k, r, response, _i, _len, _ref1, _ref2, _ref3, _ref4; |
1132 | response = original.apply(obj, arguments); | 1162 | response = original.apply(obj, arguments); |
1133 | _ref = map.pointers; | 1163 | _ref1 = map.pointers; |
1134 | for (r in _ref) { | 1164 | for (r in _ref1) { |
1135 | k = _ref[r]; | 1165 | k = _ref1[r]; |
1136 | _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; | 1166 | _ref4 = (_ref2 = (_ref3 = weakmap[r]) != null ? _ref3.callbacks[k] : void 0) != null ? _ref2 : []; |
1137 | for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | 1167 | for (_i = 0, _len = _ref4.length; _i < _len; _i++) { |
1138 | callback = _ref3[_i]; | 1168 | callback = _ref4[_i]; |
1139 | callback(); | 1169 | callback(); |
1140 | } | 1170 | } |
1141 | } | 1171 | } |
... | @@ -1163,9 +1193,9 @@ | ... | @@ -1163,9 +1193,9 @@ |
1163 | } | 1193 | } |
1164 | }, | 1194 | }, |
1165 | unobserveMutations: function(obj, ref, keypath) { | 1195 | unobserveMutations: function(obj, ref, keypath) { |
1166 | var keypaths, _ref; | 1196 | var keypaths, _ref1; |
1167 | if (Array.isArray(obj && (obj[this.id] != null))) { | 1197 | if (Array.isArray(obj && (obj[this.id] != null))) { |
1168 | if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { | 1198 | if (keypaths = (_ref1 = this.weakReference(obj).pointers) != null ? _ref1[ref] : void 0) { |
1169 | return keypaths.splice(keypaths.indexOf(keypath), 1); | 1199 | return keypaths.splice(keypaths.indexOf(keypath), 1); |
1170 | } | 1200 | } |
1171 | } | 1201 | } |
... | @@ -1182,12 +1212,12 @@ | ... | @@ -1182,12 +1212,12 @@ |
1182 | return value; | 1212 | return value; |
1183 | }, | 1213 | }, |
1184 | set: function(newValue) { | 1214 | set: function(newValue) { |
1185 | var _i, _len, _ref; | 1215 | var _i, _len, _ref1; |
1186 | if (newValue !== value) { | 1216 | if (newValue !== value) { |
1187 | value = newValue; | 1217 | value = newValue; |
1188 | _ref = callbacks[keypath]; | 1218 | _ref1 = callbacks[keypath]; |
1189 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 1219 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { |
1190 | callback = _ref[_i]; | 1220 | callback = _ref1[_i]; |
1191 | callback(); | 1221 | callback(); |
1192 | } | 1222 | } |
1193 | return _this.observeMutations(newValue, obj[_this.id], keypath); | 1223 | return _this.observeMutations(newValue, obj[_this.id], keypath); | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.6.5 | 2 | // version: 0.6.6 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function(){var a,b=function(a,b){return function(){return a.apply(b,arguments)}},c=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},d=[].slice,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},a.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},a.View=function(){function d(c,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=c,this.models=d,this.options=null!=e?e:{},this.update=b(this.update,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.select=b(this.select,this),this.build=b(this.build,this),this.componentRegExp=b(this.componentRegExp,this),this.bindingRegExp=b(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return d.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},d.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},d.prototype.build=function(){var b,d,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),e=this.componentRegExp(),d=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(c.call(h,f)<0){if(3===f.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&d("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),d("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},d.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},d.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},d.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},d.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},d.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},d.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},d}(),a.Binding=function(){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=b(this.update,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.set=b(this.set,this),this.eventHandler=b(this.eventHandler,this),this.formattedValue=b(this.formattedValue,this),this.setObserver=b(this.setObserver,this),this.setBinder=b(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return c.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},c.prototype.setObserver=function(){var b=this;return this.observer=new a.KeypathObserver(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},c.prototype.formattedValue=function(a){var b,c,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),e=b.shift(),c=this.view.formatters[e],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(d.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(d.call(b))));return a},c.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},c.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},c.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},c.prototype.publish=function(){var b,c,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],b=c.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(d.call(b))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},c.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a.KeypathObserver(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},c.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.observer.unobserve()),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},c.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},c}(),a.ComponentBinding=function(d){function e(d,e,f){var g,h,i,j,k;for(this.view=d,this.el=e,this.type=f,this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.update=b(this.update,this),this.locals=b(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,c.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return f(e,d),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(a.Binding),a.TextBinding=function(a){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=b(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return f(c,a),c.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},c.prototype.sync=function(){return c.__super__.sync.apply(this,arguments)},c}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,d){var e,f,g,h,i;for(g=[],f={"interface":d,path:""},h=0,i=a.length;i>h;h++)e=a[h],c.call(b,e)>=0?(g.push(f),f={"interface":e,path:""}):f.path+=e;return g.push(f),g},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.KeypathObserver=function(){function d(a,c,d,e){this.view=a,this.model=c,this.keypath=d,this.callback=e,this.unobserve=b(this.unobserve,this),this.realize=b(this.realize,this),this.update=b(this.update,this),this.parse=b(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return d.prototype.parse=function(){var b,d,e,f,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(d in a)g=a[d],b.push(d);return b}.call(this),h=this.keypath[0],c.call(b,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=a.KeypathParser.parse(e,b,f),this.key=this.tokens.pop()},d.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},d.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},d.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.view.adapters[c["interface"]].unsubscribe(b,c.path,this.update)):g.push(void 0);return g},d}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var d,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)d=a[e],j.push(d.selected=(h=d.value,c.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,d){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),c.call(g.pointers[b],d)<0)return g.pointers[b].push(d)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,d){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(c){var h,i,j;if(c!==f){for(f=c,j=e[b],h=0,i=j.length;i>h;h++)d=j[h],d();return g.observeMutations(c,a[g.id],b)}}})),c.call(e[b],d)<0&&e[b].push(d),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
5 | (function(){var a,b,c,d,e=function(a,b){return function(){return a.apply(b,arguments)}},f=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},g=[].slice,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},"jQuery"in window?(d="on"in jQuery?["on","off"]:["bind","unbind"],b=d[0],c=d[1],a.Util={bindEvent:function(a,c,d){return jQuery(a)[b](c,d)},unbindEvent:function(a,b,d){return jQuery(a)[c](b,d)},getInputValue:function(a){var b;return b=jQuery(a),"checkbox"===b.attr("type")?b.is(":checked"):b.val()}}):a.Util={bindEvent:function(){return"addEventListener"in window?function(a,b,c){return a.addEventListener(b,c,!1)}:function(a,b,c){return a.attachEvent("on"+b,c)}}(),unbindEvent:function(){return"removeEventListener"in window?function(a,b,c){return a.removeEventListener(b,c,!1)}:function(a,b,c){return a.detachEvent("on"+b,c)}}(),getInputValue:function(a){var b,c,d,e;if("checkbox"===a.type)return a.checked;if("select-multiple"===a.type){for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e}return a.value}},a.View=function(){function b(b,c,d){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=c,this.options=null!=d?d:{},this.update=e(this.update,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.select=e(this.select,this),this.build=e(this.build,this),this.componentRegExp=e(this.componentRegExp,this),this.bindingRegExp=e(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},b.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},b.prototype.build=function(){var b,c,d,e,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),d=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(e){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(f.call(h,e)<0){if(3===e.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(e.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),e.parentNode.insertBefore(s,e),1===t.type&&c("TextBinding",s,null,t.value);e.parentNode.removeChild(e)}}else if(d.test(e.tagName))v=e.tagName.replace(d,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,e,v));else if(null!=e.attributes){for(H=e.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=e.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||e.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),c("Binding",e,v,i.value))}for(L=function(){var a,b,c,d;for(c=e.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)e=k[i],g(e)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.Binding=function(){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.update=e(this.update,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.set=e(this.set,this),this.eventHandler=e(this.eventHandler,this),this.formattedValue=e(this.formattedValue,this),this.setBinder=e(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.model=void 0,this.setBinder()}return b.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},b.prototype.formattedValue=function(a){var b,c,d,e,f,h;for(h=this.formatters,e=0,f=h.length;f>e;e++)c=h[e],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(g.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(g.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){var b,c,d,e,f,g,h,i,j;if(this.model!==this.observer.target){for(h=this.dependencies,d=0,f=h.length;f>d;d++)c=h[d],c.unobserve();if(this.dependencies=[],null!=(this.model=this.observer.target)&&(null!=(i=this.options.dependencies)?i.length:void 0))for(j=this.options.dependencies,e=0,g=j.length;g>e;e++)b=j[e],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c)}return this.set(this.observer.value())},b.prototype.publish=function(){var b,c,d,e,f,h,i,j,k;for(e=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),f=0,h=i.length;h>f;f++)c=i[f],b=c.split(/\s+/),d=b.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(e=(k=this.view.formatters[d]).publish.apply(k,[e].concat(g.call(b))));return this.observer.publish(e)},b.prototype.bind=function(){var b,c,d,e,f,g,h;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.observer=new a.Observer(this.view,this.view.models,this.keypath,this.sync),this.model=this.observer.target,null!=this.model&&(null!=(g=this.options.dependencies)?g.length:void 0))for(h=this.options.dependencies,d=0,e=h.length;e>d;d++)b=h[d],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c);return this.view.config.preloadData?this.sync():void 0},b.prototype.unbind=function(){var a,b,c,d,e;for(null!=(d=this.binder.unbind)&&d.call(this,this.el),this.observer.unobserve(),e=this.dependencies,b=0,c=e.length;c>b;b++)a=e[b],a.unobserve();return this.dependencies=[]},b.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function c(b,c,d){var g,h,i,j,k;for(this.view=b,this.el=c,this.type=d,this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.update=e(this.update,this),this.locals=e(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,f.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return i(c,b),c.prototype.sync=function(){},c.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},c.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},c.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},c.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},c}(a.Binding),a.TextBinding=function(a){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.sync=e(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[]}return i(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var d,e,g,h,i,j;for(h=[],e={"interface":c,path:""},g=i=0,j=a.length;j>i;g=i+=1)d=a.charAt(g),f.call(b,d)>=0?(h.push(e),e={"interface":d,path:""}):e.path+=d;return h.push(e),h},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.Observer=function(){function b(a,b,c,d){this.view=a,this.model=b,this.keypath=c,this.callback=d,this.unobserve=e(this.unobserve,this),this.realize=e(this.realize,this),this.value=e(this.value,this),this.publish=e(this.publish,this),this.read=e(this.read,this),this.set=e(this.set,this),this.adapter=e(this.adapter,this),this.update=e(this.update,this),this.initialize=e(this.initialize,this),this.parse=e(this.parse,this),this.parse(),this.initialize()}return b.prototype.parse=function(){var b,c,d,e,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],f.call(b,h)>=0?(e=this.keypath[0],d=this.keypath.substr(1)):(e=this.view.config.rootInterface,d=this.keypath),this.tokens=a.KeypathParser.parse(d,b,e),this.key=this.tokens.pop()},b.prototype.initialize=function(){return this.objectPath=[],this.target=this.realize(),null!=this.target?this.set(!0,this.key,this.target,this.callback):void 0},b.prototype.update=function(){var a,b;return(a=this.realize())!==this.target&&(null!=this.target&&this.set(!1,this.key,this.target,this.callback),null!=a&&this.set(!0,this.key,a,this.callback),b=this.value(),this.target=a,this.value()!==b)?this.callback():void 0},b.prototype.adapter=function(a){return this.view.adapters[a["interface"]]},b.prototype.set=function(a,b,c,d){var e;return e=a?"subscribe":"unsubscribe",this.adapter(b)[e](c,b.path,d)},b.prototype.read=function(a,b){return this.adapter(a).read(b,a.path)},b.prototype.publish=function(a){return null!=this.target?this.adapter(this.key).publish(this.target,this.key.path,a):void 0},b.prototype.value=function(){return null!=this.target?this.read(this.key,this.target):void 0},b.prototype.realize=function(){var a,b,c,d,e,f,g,h;for(a=this.model,e=null,h=this.tokens,b=f=0,g=h.length;g>f;b=++f)d=h[b],null!=a?(null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.set(!1,d,c,this.update),this.set(!0,d,a,this.update),this.objectPath[b]=a):(this.set(!0,d,a,this.update),this.objectPath[b]=a),a=this.read(d,a)):(null==e&&(e=b),(c=this.objectPath[b])&&this.set(!1,d,c,this.update));return null!=e&&this.objectPath.splice(e),a},b.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.set(!1,c,b,this.update)):g.push(void 0);return g},b}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c,d,e,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],d=0,e=a.length;e>d;d++)c=a[d],j.push(c.selected=(h=c.value,f.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var d,e,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},e=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=e.length;j>i;i++)d=e[i],this.stubFunction(a,d);if(null==(h=g.pointers)[b]&&(h[b]=[]),f.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var d,e,g=this;return d=this.weakReference(a).callbacks,null==d[b]&&(d[b]=[],e=a[b],Object.defineProperty(a,b,{get:function(){return e},set:function(f){var h,i,j;if(f!==e){for(e=f,j=d[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(f,a[g.id],b)}}})),f.call(d[b],c)<0&&d[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "description": "Declarative data binding + templating solution.", | 3 | "description": "Declarative data binding + templating solution.", |
4 | "version": "0.6.5", | 4 | "version": "0.6.6", |
5 | "author": "Michael Richards", | 5 | "author": "Michael Richards", |
6 | "url": "http://rivetsjs.com", | 6 | "url": "http://rivetsjs.com", |
7 | "main": "./dist/rivets.js", | 7 | "main": "./dist/rivets.js", | ... | ... |
-
Please register or sign in to post a comment