Build 0.6.0.
Showing
4 changed files
with
698 additions
and
479 deletions
1 | { | 1 | { |
2 | "name": "rivets", | 2 | "name": "rivets", |
3 | "repo": "mikeric/rivets", | 3 | "repo": "mikeric/rivets", |
4 | "description": "Declarative data binding facility.", | 4 | "description": "Declarative data binding + templating solution.", |
5 | "version": "0.5.13", | 5 | "version": "0.6.0", |
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.5.13 | 2 | // version: 0.6.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
6 | var Rivets, jQuery, | 6 | var KeypathObserver, Rivets, |
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 | __slice = [].slice, | 9 | __slice = [].slice, |
9 | __hasProp = {}.hasOwnProperty, | 10 | __hasProp = {}.hasOwnProperty, |
10 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | 11 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; |
11 | __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; }; | ||
12 | 12 | ||
13 | Rivets = {}; | 13 | Rivets = { |
14 | binders: {}, | ||
15 | components: {}, | ||
16 | formatters: {}, | ||
17 | adapters: {}, | ||
18 | config: { | ||
19 | prefix: 'rv', | ||
20 | templateDelimiters: ['{', '}'], | ||
21 | rootInterface: '.', | ||
22 | preloadData: true, | ||
23 | handler: function(context, ev, binding) { | ||
24 | return this.call(context, ev, binding.view.models); | ||
25 | } | ||
26 | } | ||
27 | }; | ||
14 | 28 | ||
15 | jQuery = window.jQuery || window.Zepto; | 29 | Rivets.Util = { |
30 | bindEvent: function(el, event, handler) { | ||
31 | if (window.jQuery != null) { | ||
32 | el = jQuery(el); | ||
33 | if (el.on != null) { | ||
34 | return el.on(event, handler); | ||
35 | } else { | ||
36 | return el.bind(event, handler); | ||
37 | } | ||
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 | } | ||
44 | }, | ||
45 | unbindEvent: function(el, event, handler) { | ||
46 | if (window.jQuery != null) { | ||
47 | el = jQuery(el); | ||
48 | if (el.off != null) { | ||
49 | return el.off(event, handler); | ||
50 | } else { | ||
51 | return el.unbind(event, handler); | ||
52 | } | ||
53 | } else if (window.removeEventListener != null) { | ||
54 | return el.removeEventListener(event, handler, false); | ||
55 | } else { | ||
56 | event = 'on' + event; | ||
57 | return el.detachEvent(event, handler); | ||
58 | } | ||
59 | }, | ||
60 | getInputValue: function(el) { | ||
61 | var o, _i, _len, _results; | ||
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 | } | ||
70 | } else { | ||
71 | switch (el.type) { | ||
72 | case 'checkbox': | ||
73 | return el.checked; | ||
74 | case 'select-multiple': | ||
75 | _results = []; | ||
76 | for (_i = 0, _len = el.length; _i < _len; _i++) { | ||
77 | o = el[_i]; | ||
78 | if (o.selected) { | ||
79 | _results.push(o.value); | ||
80 | } | ||
81 | } | ||
82 | return _results; | ||
83 | break; | ||
84 | default: | ||
85 | return el.value; | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | }; | ||
16 | 90 | ||
17 | if (!String.prototype.trim) { | 91 | Rivets.View = (function() { |
18 | String.prototype.trim = function() { | 92 | function View(els, models, options) { |
19 | return this.replace(/^\s+|\s+$/g, ''); | 93 | var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; |
94 | this.els = els; | ||
95 | this.models = models; | ||
96 | this.options = options != null ? options : {}; | ||
97 | this.update = __bind(this.update, this); | ||
98 | this.publish = __bind(this.publish, this); | ||
99 | this.sync = __bind(this.sync, this); | ||
100 | this.unbind = __bind(this.unbind, this); | ||
101 | this.bind = __bind(this.bind, this); | ||
102 | this.select = __bind(this.select, this); | ||
103 | this.build = __bind(this.build, this); | ||
104 | this.componentRegExp = __bind(this.componentRegExp, this); | ||
105 | this.bindingRegExp = __bind(this.bindingRegExp, this); | ||
106 | if (!(this.els.jquery || this.els instanceof Array)) { | ||
107 | this.els = [this.els]; | ||
108 | } | ||
109 | _ref = ['config', 'binders', 'formatters', 'adapters']; | ||
110 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
111 | option = _ref[_i]; | ||
112 | this[option] = {}; | ||
113 | if (this.options[option]) { | ||
114 | _ref1 = this.options[option]; | ||
115 | for (k in _ref1) { | ||
116 | v = _ref1[k]; | ||
117 | this[option][k] = v; | ||
118 | } | ||
119 | } | ||
120 | _ref2 = Rivets[option]; | ||
121 | for (k in _ref2) { | ||
122 | v = _ref2[k]; | ||
123 | if ((_base = this[option])[k] == null) { | ||
124 | _base[k] = v; | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | this.build(); | ||
129 | } | ||
130 | |||
131 | View.prototype.bindingRegExp = function() { | ||
132 | return new RegExp("^" + this.config.prefix + "-"); | ||
133 | }; | ||
134 | |||
135 | View.prototype.componentRegExp = function() { | ||
136 | return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); | ||
20 | }; | 137 | }; |
138 | |||
139 | View.prototype.build = function() { | ||
140 | var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, | ||
141 | _this = this; | ||
142 | this.bindings = []; | ||
143 | skipNodes = []; | ||
144 | bindingRegExp = this.bindingRegExp(); | ||
145 | componentRegExp = this.componentRegExp(); | ||
146 | buildBinding = function(binding, node, type, declaration) { | ||
147 | var context, ctx, dependencies, keypath, options, pipe, pipes; | ||
148 | options = {}; | ||
149 | pipes = (function() { | ||
150 | var _i, _len, _ref, _results; | ||
151 | _ref = declaration.split('|'); | ||
152 | _results = []; | ||
153 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
154 | pipe = _ref[_i]; | ||
155 | _results.push(pipe.trim()); | ||
156 | } | ||
157 | return _results; | ||
158 | })(); | ||
159 | context = (function() { | ||
160 | var _i, _len, _ref, _results; | ||
161 | _ref = pipes.shift().split('<'); | ||
162 | _results = []; | ||
163 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
164 | ctx = _ref[_i]; | ||
165 | _results.push(ctx.trim()); | ||
21 | } | 166 | } |
167 | return _results; | ||
168 | })(); | ||
169 | keypath = context.shift(); | ||
170 | options.formatters = pipes; | ||
171 | if (dependencies = context.shift()) { | ||
172 | options.dependencies = dependencies.split(/\s+/); | ||
173 | } | ||
174 | return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); | ||
175 | }; | ||
176 | parse = function(node) { | ||
177 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results; | ||
178 | if (__indexOf.call(skipNodes, node) < 0) { | ||
179 | if (node.nodeType === Node.TEXT_NODE) { | ||
180 | parser = Rivets.TextTemplateParser; | ||
181 | if (delimiters = _this.config.templateDelimiters) { | ||
182 | if ((tokens = parser.parse(node.data, delimiters)).length) { | ||
183 | if (!(tokens.length === 1 && tokens[0].type === parser.types.text)) { | ||
184 | startToken = tokens[0], restTokens = 2 <= tokens.length ? __slice.call(tokens, 1) : []; | ||
185 | node.data = startToken.value; | ||
186 | if (startToken.type === 0) { | ||
187 | node.data = startToken.value; | ||
188 | } else { | ||
189 | buildBinding('TextBinding', node, null, startToken.value); | ||
190 | } | ||
191 | for (_i = 0, _len = restTokens.length; _i < _len; _i++) { | ||
192 | token = restTokens[_i]; | ||
193 | text = document.createTextNode(token.value); | ||
194 | node.parentNode.appendChild(text); | ||
195 | if (token.type === 1) { | ||
196 | buildBinding('TextBinding', text, null, token.value); | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | } else if (componentRegExp.test(node.tagName)) { | ||
203 | type = node.tagName.replace(componentRegExp, '').toLowerCase(); | ||
204 | _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); | ||
205 | } else if (node.attributes != null) { | ||
206 | _ref = node.attributes; | ||
207 | for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | ||
208 | attribute = _ref[_j]; | ||
209 | if (bindingRegExp.test(attribute.name)) { | ||
210 | type = attribute.name.replace(bindingRegExp, ''); | ||
211 | if (!(binder = _this.binders[type])) { | ||
212 | _ref1 = _this.binders; | ||
213 | for (identifier in _ref1) { | ||
214 | value = _ref1[identifier]; | ||
215 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
216 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | ||
217 | if (regexp.test(type)) { | ||
218 | binder = value; | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | } | ||
223 | binder || (binder = _this.binders['*']); | ||
224 | if (binder.block) { | ||
225 | _ref2 = node.childNodes; | ||
226 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | ||
227 | n = _ref2[_k]; | ||
228 | skipNodes.push(n); | ||
229 | } | ||
230 | attributes = [attribute]; | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | _ref3 = attributes || node.attributes; | ||
235 | for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { | ||
236 | attribute = _ref3[_l]; | ||
237 | if (bindingRegExp.test(attribute.name)) { | ||
238 | type = attribute.name.replace(bindingRegExp, ''); | ||
239 | buildBinding('Binding', node, type, attribute.value); | ||
240 | } | ||
241 | } | ||
242 | } | ||
243 | _ref4 = node.childNodes; | ||
244 | _results = []; | ||
245 | for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) { | ||
246 | childNode = _ref4[_m]; | ||
247 | _results.push(parse(childNode)); | ||
248 | } | ||
249 | return _results; | ||
250 | } | ||
251 | }; | ||
252 | _ref = this.els; | ||
253 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
254 | el = _ref[_i]; | ||
255 | parse(el); | ||
256 | } | ||
257 | }; | ||
258 | |||
259 | View.prototype.select = function(fn) { | ||
260 | var binding, _i, _len, _ref, _results; | ||
261 | _ref = this.bindings; | ||
262 | _results = []; | ||
263 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
264 | binding = _ref[_i]; | ||
265 | if (fn(binding)) { | ||
266 | _results.push(binding); | ||
267 | } | ||
268 | } | ||
269 | return _results; | ||
270 | }; | ||
271 | |||
272 | View.prototype.bind = function() { | ||
273 | var binding, _i, _len, _ref, _results; | ||
274 | _ref = this.bindings; | ||
275 | _results = []; | ||
276 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
277 | binding = _ref[_i]; | ||
278 | _results.push(binding.bind()); | ||
279 | } | ||
280 | return _results; | ||
281 | }; | ||
282 | |||
283 | View.prototype.unbind = function() { | ||
284 | var binding, _i, _len, _ref, _results; | ||
285 | _ref = this.bindings; | ||
286 | _results = []; | ||
287 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
288 | binding = _ref[_i]; | ||
289 | _results.push(binding.unbind()); | ||
290 | } | ||
291 | return _results; | ||
292 | }; | ||
293 | |||
294 | View.prototype.sync = function() { | ||
295 | var binding, _i, _len, _ref, _results; | ||
296 | _ref = this.bindings; | ||
297 | _results = []; | ||
298 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
299 | binding = _ref[_i]; | ||
300 | _results.push(binding.sync()); | ||
301 | } | ||
302 | return _results; | ||
303 | }; | ||
304 | |||
305 | View.prototype.publish = function() { | ||
306 | var binding, _i, _len, _ref, _results; | ||
307 | _ref = this.select(function(b) { | ||
308 | return b.binder.publishes; | ||
309 | }); | ||
310 | _results = []; | ||
311 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
312 | binding = _ref[_i]; | ||
313 | _results.push(binding.publish()); | ||
314 | } | ||
315 | return _results; | ||
316 | }; | ||
317 | |||
318 | View.prototype.update = function(models) { | ||
319 | var binding, key, model, _i, _len, _ref, _results; | ||
320 | if (models == null) { | ||
321 | models = {}; | ||
322 | } | ||
323 | for (key in models) { | ||
324 | model = models[key]; | ||
325 | this.models[key] = model; | ||
326 | } | ||
327 | _ref = this.bindings; | ||
328 | _results = []; | ||
329 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
330 | binding = _ref[_i]; | ||
331 | _results.push(binding.update(models)); | ||
332 | } | ||
333 | return _results; | ||
334 | }; | ||
335 | |||
336 | return View; | ||
337 | |||
338 | })(); | ||
22 | 339 | ||
23 | Rivets.Binding = (function() { | 340 | Rivets.Binding = (function() { |
24 | function Binding(view, el, type, key, keypath, options) { | 341 | function Binding(view, el, type, keypath, options) { |
25 | var identifier, regexp, value, _ref; | ||
26 | this.view = view; | 342 | this.view = view; |
27 | this.el = el; | 343 | this.el = el; |
28 | this.type = type; | 344 | this.type = type; |
29 | this.key = key; | ||
30 | this.keypath = keypath; | 345 | this.keypath = keypath; |
31 | this.options = options != null ? options : {}; | 346 | this.options = options != null ? options : {}; |
32 | this.update = __bind(this.update, this); | 347 | this.update = __bind(this.update, this); |
... | @@ -37,15 +352,25 @@ | ... | @@ -37,15 +352,25 @@ |
37 | this.set = __bind(this.set, this); | 352 | this.set = __bind(this.set, this); |
38 | this.eventHandler = __bind(this.eventHandler, this); | 353 | this.eventHandler = __bind(this.eventHandler, this); |
39 | this.formattedValue = __bind(this.formattedValue, this); | 354 | this.formattedValue = __bind(this.formattedValue, this); |
40 | if (!(this.binder = this.view.binders[type])) { | 355 | this.setObserver = __bind(this.setObserver, this); |
356 | this.setBinder = __bind(this.setBinder, this); | ||
357 | this.formatters = this.options.formatters || []; | ||
358 | this.dependencies = []; | ||
359 | this.setBinder(); | ||
360 | this.setObserver(); | ||
361 | } | ||
362 | |||
363 | Binding.prototype.setBinder = function() { | ||
364 | var identifier, regexp, value, _ref; | ||
365 | if (!(this.binder = this.view.binders[this.type])) { | ||
41 | _ref = this.view.binders; | 366 | _ref = this.view.binders; |
42 | for (identifier in _ref) { | 367 | for (identifier in _ref) { |
43 | value = _ref[identifier]; | 368 | value = _ref[identifier]; |
44 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | 369 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { |
45 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | 370 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); |
46 | if (regexp.test(type)) { | 371 | if (regexp.test(this.type)) { |
47 | this.binder = value; | 372 | this.binder = value; |
48 | this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type); | 373 | this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); |
49 | this.args.shift(); | 374 | this.args.shift(); |
50 | } | 375 | } |
51 | } | 376 | } |
... | @@ -53,13 +378,27 @@ | ... | @@ -53,13 +378,27 @@ |
53 | } | 378 | } |
54 | this.binder || (this.binder = this.view.binders['*']); | 379 | this.binder || (this.binder = this.view.binders['*']); |
55 | if (this.binder instanceof Function) { | 380 | if (this.binder instanceof Function) { |
56 | this.binder = { | 381 | return this.binder = { |
57 | routine: this.binder | 382 | routine: this.binder |
58 | }; | 383 | }; |
59 | } | 384 | } |
60 | this.formatters = this.options.formatters || []; | 385 | }; |
61 | this.model = this.key ? this.view.models[this.key] : this.view.models; | 386 | |
387 | Binding.prototype.setObserver = function() { | ||
388 | var _this = this; | ||
389 | this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { | ||
390 | if (_this.key) { | ||
391 | _this.unbind(true); | ||
392 | } | ||
393 | _this.model = obs.target; | ||
394 | if (_this.key) { | ||
395 | _this.bind(true); | ||
62 | } | 396 | } |
397 | return _this.sync(); | ||
398 | }); | ||
399 | this.key = this.observer.key; | ||
400 | return this.model = this.observer.target; | ||
401 | }; | ||
63 | 402 | ||
64 | Binding.prototype.formattedValue = function(value) { | 403 | Binding.prototype.formattedValue = function(value) { |
65 | var args, formatter, id, _i, _len, _ref; | 404 | var args, formatter, id, _i, _len, _ref; |
... | @@ -68,7 +407,7 @@ | ... | @@ -68,7 +407,7 @@ |
68 | formatter = _ref[_i]; | 407 | formatter = _ref[_i]; |
69 | args = formatter.split(/\s+/); | 408 | args = formatter.split(/\s+/); |
70 | id = args.shift(); | 409 | id = args.shift(); |
71 | formatter = this.model[id] instanceof Function ? this.model[id] : this.view.formatters[id]; | 410 | formatter = this.view.formatters[id]; |
72 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { | 411 | if ((formatter != null ? formatter.read : void 0) instanceof Function) { |
73 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); | 412 | value = formatter.read.apply(formatter, [value].concat(__slice.call(args))); |
74 | } else if (formatter instanceof Function) { | 413 | } else if (formatter instanceof Function) { |
... | @@ -93,7 +432,7 @@ | ... | @@ -93,7 +432,7 @@ |
93 | }; | 432 | }; |
94 | 433 | ||
95 | Binding.prototype.sync = function() { | 434 | Binding.prototype.sync = function() { |
96 | return this.set(this.options.bypass ? this.model[this.keypath] : this.view.config.adapter.read(this.model, this.keypath)); | 435 | return this.set(this.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); |
97 | }; | 436 | }; |
98 | 437 | ||
99 | Binding.prototype.publish = function() { | 438 | Binding.prototype.publish = function() { |
... | @@ -108,65 +447,67 @@ | ... | @@ -108,65 +447,67 @@ |
108 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); | 447 | value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args))); |
109 | } | 448 | } |
110 | } | 449 | } |
111 | return this.view.config.adapter.publish(this.model, this.keypath, value); | 450 | return this.view.adapters[this.key["interface"]].publish(this.model, this.key.path, value); |
112 | }; | 451 | }; |
113 | 452 | ||
114 | Binding.prototype.bind = function() { | 453 | Binding.prototype.bind = function(silent) { |
115 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 454 | var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, |
455 | _this = this; | ||
456 | if (silent == null) { | ||
457 | silent = false; | ||
458 | } | ||
459 | if (!silent) { | ||
116 | if ((_ref = this.binder.bind) != null) { | 460 | if ((_ref = this.binder.bind) != null) { |
117 | _ref.call(this, this.el); | 461 | _ref.call(this, this.el); |
118 | } | 462 | } |
119 | if (this.options.bypass) { | ||
120 | this.sync(); | ||
121 | } else { | ||
122 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
123 | if (this.view.config.preloadData) { | ||
124 | this.sync(); | ||
125 | } | 463 | } |
464 | if (this.key) { | ||
465 | this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); | ||
466 | } | ||
467 | if (!silent ? this.view.config.preloadData : void 0) { | ||
468 | this.sync(); | ||
126 | } | 469 | } |
127 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | 470 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { |
128 | _ref2 = this.options.dependencies; | 471 | _ref2 = this.options.dependencies; |
129 | _results = []; | 472 | _results = []; |
130 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | 473 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { |
131 | dependency = _ref2[_i]; | 474 | dependency = _ref2[_i]; |
132 | if (/^\./.test(dependency)) { | 475 | observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { |
133 | model = this.model; | 476 | var key; |
134 | keypath = dependency.substr(1); | 477 | key = obs.key; |
135 | } else { | 478 | _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); |
136 | dependency = dependency.split('.'); | 479 | _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); |
137 | model = this.view.models[dependency.shift()]; | 480 | return _this.sync(); |
138 | keypath = dependency.join('.'); | 481 | }); |
139 | } | 482 | key = observer.key; |
140 | _results.push(this.view.config.adapter.subscribe(model, keypath, this.sync)); | 483 | this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); |
484 | _results.push(this.dependencies.push(observer)); | ||
141 | } | 485 | } |
142 | return _results; | 486 | return _results; |
143 | } | 487 | } |
144 | }; | 488 | }; |
145 | 489 | ||
146 | Binding.prototype.unbind = function() { | 490 | Binding.prototype.unbind = function(silent) { |
147 | var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results; | 491 | var key, obs, _i, _len, _ref, _ref1; |
492 | if (silent == null) { | ||
493 | silent = false; | ||
494 | } | ||
495 | if (!silent) { | ||
148 | if ((_ref = this.binder.unbind) != null) { | 496 | if ((_ref = this.binder.unbind) != null) { |
149 | _ref.call(this, this.el); | 497 | _ref.call(this, this.el); |
150 | } | 498 | } |
151 | if (!this.options.bypass) { | ||
152 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | ||
153 | } | 499 | } |
154 | if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) { | 500 | if (this.key) { |
155 | _ref2 = this.options.dependencies; | 501 | this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); |
156 | _results = []; | ||
157 | for (_i = 0, _len = _ref2.length; _i < _len; _i++) { | ||
158 | dependency = _ref2[_i]; | ||
159 | if (/^\./.test(dependency)) { | ||
160 | model = this.model; | ||
161 | keypath = dependency.substr(1); | ||
162 | } else { | ||
163 | dependency = dependency.split('.'); | ||
164 | model = this.view.models[dependency.shift()]; | ||
165 | keypath = dependency.join('.'); | ||
166 | } | 502 | } |
167 | _results.push(this.view.config.adapter.unsubscribe(model, keypath, this.sync)); | 503 | if (this.dependencies.length) { |
504 | _ref1 = this.dependencies; | ||
505 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
506 | obs = _ref1[_i]; | ||
507 | key = obs.key; | ||
508 | this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); | ||
168 | } | 509 | } |
169 | return _results; | 510 | return this.dependencies = []; |
170 | } | 511 | } |
171 | }; | 512 | }; |
172 | 513 | ||
... | @@ -175,24 +516,6 @@ | ... | @@ -175,24 +516,6 @@ |
175 | if (models == null) { | 516 | if (models == null) { |
176 | models = {}; | 517 | models = {}; |
177 | } | 518 | } |
178 | if (this.key) { | ||
179 | if (models[this.key]) { | ||
180 | if (!this.options.bypass) { | ||
181 | this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync); | ||
182 | } | ||
183 | this.model = models[this.key]; | ||
184 | if (this.options.bypass) { | ||
185 | this.sync(); | ||
186 | } else { | ||
187 | this.view.config.adapter.subscribe(this.model, this.keypath, this.sync); | ||
188 | if (this.view.config.preloadData) { | ||
189 | this.sync(); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | } else { | ||
194 | this.sync(); | ||
195 | } | ||
196 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; | 519 | return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; |
197 | }; | 520 | }; |
198 | 521 | ||
... | @@ -246,328 +569,93 @@ | ... | @@ -246,328 +569,93 @@ |
246 | for (key in models) { | 569 | for (key in models) { |
247 | model = models[key]; | 570 | model = models[key]; |
248 | if (result[key] == null) { | 571 | if (result[key] == null) { |
249 | result[key] = model; | 572 | result[key] = model; |
250 | } | 573 | } |
251 | } | ||
252 | return result; | ||
253 | }; | ||
254 | |||
255 | ComponentBinding.prototype.update = function(models) { | ||
256 | var _ref; | ||
257 | return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; | ||
258 | }; | ||
259 | |||
260 | ComponentBinding.prototype.bind = function() { | ||
261 | var el, _ref; | ||
262 | if (this.componentView != null) { | ||
263 | return (_ref = this.componentView) != null ? _ref.bind() : void 0; | ||
264 | } else { | ||
265 | el = this.component.build.call(this.attributes); | ||
266 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); | ||
267 | return this.el.parentNode.replaceChild(el, this.el); | ||
268 | } | ||
269 | }; | ||
270 | |||
271 | ComponentBinding.prototype.unbind = function() { | ||
272 | var _ref; | ||
273 | return (_ref = this.componentView) != null ? _ref.unbind() : void 0; | ||
274 | }; | ||
275 | |||
276 | return ComponentBinding; | ||
277 | |||
278 | })(Rivets.Binding); | ||
279 | |||
280 | Rivets.TextBinding = (function(_super) { | ||
281 | __extends(TextBinding, _super); | ||
282 | |||
283 | function TextBinding(view, el, type, key, keypath, options) { | ||
284 | this.view = view; | ||
285 | this.el = el; | ||
286 | this.type = type; | ||
287 | this.key = key; | ||
288 | this.keypath = keypath; | ||
289 | this.options = options != null ? options : {}; | ||
290 | this.sync = __bind(this.sync, this); | ||
291 | this.formatters = this.options.formatters || []; | ||
292 | this.model = this.key ? this.view.models[this.key] : this.view.models; | ||
293 | } | ||
294 | |||
295 | TextBinding.prototype.binder = { | ||
296 | routine: function(node, value) { | ||
297 | return node.data = value != null ? value : ''; | ||
298 | } | ||
299 | }; | ||
300 | |||
301 | TextBinding.prototype.sync = function() { | ||
302 | return TextBinding.__super__.sync.apply(this, arguments); | ||
303 | }; | ||
304 | |||
305 | return TextBinding; | ||
306 | |||
307 | })(Rivets.Binding); | ||
308 | |||
309 | Rivets.View = (function() { | ||
310 | function View(els, models, options) { | ||
311 | var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; | ||
312 | this.els = els; | ||
313 | this.models = models; | ||
314 | this.options = options != null ? options : {}; | ||
315 | this.update = __bind(this.update, this); | ||
316 | this.publish = __bind(this.publish, this); | ||
317 | this.sync = __bind(this.sync, this); | ||
318 | this.unbind = __bind(this.unbind, this); | ||
319 | this.bind = __bind(this.bind, this); | ||
320 | this.select = __bind(this.select, this); | ||
321 | this.build = __bind(this.build, this); | ||
322 | this.componentRegExp = __bind(this.componentRegExp, this); | ||
323 | this.bindingRegExp = __bind(this.bindingRegExp, this); | ||
324 | if (typeof this.els.length === 'undefined') { | ||
325 | this.els = [this.els]; | ||
326 | } | ||
327 | _ref = ['config', 'binders', 'formatters']; | ||
328 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
329 | option = _ref[_i]; | ||
330 | this[option] = {}; | ||
331 | if (this.options[option]) { | ||
332 | _ref1 = this.options[option]; | ||
333 | for (k in _ref1) { | ||
334 | v = _ref1[k]; | ||
335 | this[option][k] = v; | ||
336 | } | ||
337 | } | ||
338 | _ref2 = Rivets[option]; | ||
339 | for (k in _ref2) { | ||
340 | v = _ref2[k]; | ||
341 | if ((_base = this[option])[k] == null) { | ||
342 | _base[k] = v; | ||
343 | } | ||
344 | } | ||
345 | } | ||
346 | this.build(); | ||
347 | } | ||
348 | |||
349 | View.prototype.bindingRegExp = function() { | ||
350 | var prefix; | ||
351 | prefix = this.config.prefix; | ||
352 | if (prefix) { | ||
353 | return new RegExp("^data-" + prefix + "-"); | ||
354 | } else { | ||
355 | return /^data-/; | ||
356 | } | ||
357 | }; | ||
358 | |||
359 | View.prototype.componentRegExp = function() { | ||
360 | var _ref, _ref1; | ||
361 | return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); | ||
362 | }; | ||
363 | |||
364 | View.prototype.build = function() { | ||
365 | var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, | ||
366 | _this = this; | ||
367 | this.bindings = []; | ||
368 | skipNodes = []; | ||
369 | bindingRegExp = this.bindingRegExp(); | ||
370 | componentRegExp = this.componentRegExp(); | ||
371 | buildBinding = function(binding, node, type, declaration) { | ||
372 | var context, ctx, dependencies, key, keypath, options, path, pipe, pipes, splitPath; | ||
373 | options = {}; | ||
374 | pipes = (function() { | ||
375 | var _i, _len, _ref, _results; | ||
376 | _ref = declaration.split('|'); | ||
377 | _results = []; | ||
378 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
379 | pipe = _ref[_i]; | ||
380 | _results.push(pipe.trim()); | ||
381 | } | ||
382 | return _results; | ||
383 | })(); | ||
384 | context = (function() { | ||
385 | var _i, _len, _ref, _results; | ||
386 | _ref = pipes.shift().split('<'); | ||
387 | _results = []; | ||
388 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
389 | ctx = _ref[_i]; | ||
390 | _results.push(ctx.trim()); | ||
391 | } | ||
392 | return _results; | ||
393 | })(); | ||
394 | path = context.shift(); | ||
395 | splitPath = path.split(/\.|:/); | ||
396 | options.formatters = pipes; | ||
397 | options.bypass = path.indexOf(':') !== -1; | ||
398 | if (splitPath[0]) { | ||
399 | key = splitPath.shift(); | ||
400 | } else { | ||
401 | key = null; | ||
402 | splitPath.shift(); | ||
403 | } | ||
404 | keypath = splitPath.join('.'); | ||
405 | if (dependencies = context.shift()) { | ||
406 | options.dependencies = dependencies.split(/\s+/); | ||
407 | } | ||
408 | return _this.bindings.push(new Rivets[binding](_this, node, type, key, keypath, options)); | ||
409 | }; | ||
410 | parse = function(node) { | ||
411 | var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results; | ||
412 | if (__indexOf.call(skipNodes, node) < 0) { | ||
413 | if (node.nodeType === Node.TEXT_NODE) { | ||
414 | parser = Rivets.TextTemplateParser; | ||
415 | if (delimiters = _this.config.templateDelimiters) { | ||
416 | if ((tokens = parser.parse(node.data, delimiters)).length) { | ||
417 | if (!(tokens.length === 1 && tokens[0].type === parser.types.text)) { | ||
418 | startToken = tokens[0], restTokens = 2 <= tokens.length ? __slice.call(tokens, 1) : []; | ||
419 | node.data = startToken.value; | ||
420 | if (startToken.type === 0) { | ||
421 | node.data = startToken.value; | ||
422 | } else { | ||
423 | buildBinding('TextBinding', node, null, startToken.value); | ||
424 | } | ||
425 | for (_i = 0, _len = restTokens.length; _i < _len; _i++) { | ||
426 | token = restTokens[_i]; | ||
427 | text = document.createTextNode(token.value); | ||
428 | node.parentNode.appendChild(text); | ||
429 | if (token.type === 1) { | ||
430 | buildBinding('TextBinding', text, null, token.value); | ||
431 | } | ||
432 | } | ||
433 | } | ||
434 | } | ||
435 | } | ||
436 | } else if (componentRegExp.test(node.tagName)) { | ||
437 | type = node.tagName.replace(componentRegExp, '').toLowerCase(); | ||
438 | _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); | ||
439 | } else if (node.attributes != null) { | ||
440 | _ref = node.attributes; | ||
441 | for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { | ||
442 | attribute = _ref[_j]; | ||
443 | if (bindingRegExp.test(attribute.name)) { | ||
444 | type = attribute.name.replace(bindingRegExp, ''); | ||
445 | if (!(binder = _this.binders[type])) { | ||
446 | _ref1 = _this.binders; | ||
447 | for (identifier in _ref1) { | ||
448 | value = _ref1[identifier]; | ||
449 | if (identifier !== '*' && identifier.indexOf('*') !== -1) { | ||
450 | regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); | ||
451 | if (regexp.test(type)) { | ||
452 | binder = value; | ||
453 | } | ||
454 | } | ||
455 | } | ||
456 | } | ||
457 | binder || (binder = _this.binders['*']); | ||
458 | if (binder.block) { | ||
459 | _ref2 = node.childNodes; | ||
460 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { | ||
461 | n = _ref2[_k]; | ||
462 | skipNodes.push(n); | ||
463 | } | ||
464 | attributes = [attribute]; | ||
465 | } | ||
466 | } | ||
467 | } | ||
468 | _ref3 = attributes || node.attributes; | ||
469 | for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) { | ||
470 | attribute = _ref3[_l]; | ||
471 | if (bindingRegExp.test(attribute.name)) { | ||
472 | type = attribute.name.replace(bindingRegExp, ''); | ||
473 | buildBinding('Binding', node, type, attribute.value); | ||
474 | } | ||
475 | } | ||
476 | } | ||
477 | _ref4 = node.childNodes; | ||
478 | _results = []; | ||
479 | for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) { | ||
480 | childNode = _ref4[_m]; | ||
481 | _results.push(parse(childNode)); | ||
482 | } | ||
483 | return _results; | ||
484 | } | ||
485 | }; | ||
486 | _ref = this.els; | ||
487 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
488 | el = _ref[_i]; | ||
489 | parse(el); | ||
490 | } | 574 | } |
575 | return result; | ||
491 | }; | 576 | }; |
492 | 577 | ||
493 | View.prototype.select = function(fn) { | 578 | ComponentBinding.prototype.update = function(models) { |
494 | var binding, _i, _len, _ref, _results; | 579 | var _ref; |
495 | _ref = this.bindings; | 580 | return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; |
496 | _results = []; | ||
497 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
498 | binding = _ref[_i]; | ||
499 | if (fn(binding)) { | ||
500 | _results.push(binding); | ||
501 | } | ||
502 | } | ||
503 | return _results; | ||
504 | }; | 581 | }; |
505 | 582 | ||
506 | View.prototype.bind = function() { | 583 | ComponentBinding.prototype.bind = function() { |
507 | var binding, _i, _len, _ref, _results; | 584 | var el, _ref; |
508 | _ref = this.bindings; | 585 | if (this.componentView != null) { |
509 | _results = []; | 586 | return (_ref = this.componentView) != null ? _ref.bind() : void 0; |
510 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 587 | } else { |
511 | binding = _ref[_i]; | 588 | el = this.component.build.call(this.attributes); |
512 | _results.push(binding.bind()); | 589 | (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); |
590 | return this.el.parentNode.replaceChild(el, this.el); | ||
513 | } | 591 | } |
514 | return _results; | ||
515 | }; | 592 | }; |
516 | 593 | ||
517 | View.prototype.unbind = function() { | 594 | ComponentBinding.prototype.unbind = function() { |
518 | var binding, _i, _len, _ref, _results; | 595 | var _ref; |
519 | _ref = this.bindings; | 596 | return (_ref = this.componentView) != null ? _ref.unbind() : void 0; |
520 | _results = []; | ||
521 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
522 | binding = _ref[_i]; | ||
523 | _results.push(binding.unbind()); | ||
524 | } | ||
525 | return _results; | ||
526 | }; | 597 | }; |
527 | 598 | ||
528 | View.prototype.sync = function() { | 599 | return ComponentBinding; |
529 | var binding, _i, _len, _ref, _results; | 600 | |
530 | _ref = this.bindings; | 601 | })(Rivets.Binding); |
531 | _results = []; | 602 | |
532 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 603 | Rivets.TextBinding = (function(_super) { |
533 | binding = _ref[_i]; | 604 | __extends(TextBinding, _super); |
534 | _results.push(binding.sync()); | 605 | |
606 | function TextBinding(view, el, type, keypath, options) { | ||
607 | this.view = view; | ||
608 | this.el = el; | ||
609 | this.type = type; | ||
610 | this.keypath = keypath; | ||
611 | this.options = options != null ? options : {}; | ||
612 | this.sync = __bind(this.sync, this); | ||
613 | this.formatters = this.options.formatters || []; | ||
614 | this.dependencies = []; | ||
615 | this.setObserver(); | ||
535 | } | 616 | } |
536 | return _results; | ||
537 | }; | ||
538 | 617 | ||
539 | View.prototype.publish = function() { | 618 | TextBinding.prototype.binder = { |
540 | var binding, _i, _len, _ref, _results; | 619 | routine: function(node, value) { |
541 | _ref = this.select(function(b) { | 620 | return node.data = value != null ? value : ''; |
542 | return b.binder.publishes; | ||
543 | }); | ||
544 | _results = []; | ||
545 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
546 | binding = _ref[_i]; | ||
547 | _results.push(binding.publish()); | ||
548 | } | 621 | } |
549 | return _results; | ||
550 | }; | 622 | }; |
551 | 623 | ||
552 | View.prototype.update = function(models) { | 624 | TextBinding.prototype.sync = function() { |
553 | var binding, key, model, _i, _len, _ref, _results; | 625 | return TextBinding.__super__.sync.apply(this, arguments); |
554 | if (models == null) { | 626 | }; |
555 | models = {}; | 627 | |
556 | } | 628 | return TextBinding; |
557 | for (key in models) { | 629 | |
558 | model = models[key]; | 630 | })(Rivets.Binding); |
559 | this.models[key] = model; | 631 | |
632 | Rivets.KeypathParser = (function() { | ||
633 | function KeypathParser() {} | ||
634 | |||
635 | KeypathParser.parse = function(keypath, interfaces, root) { | ||
636 | var char, current, index, tokens; | ||
637 | tokens = []; | ||
638 | current = { | ||
639 | "interface": root, | ||
640 | path: '' | ||
641 | }; | ||
642 | for (index in keypath) { | ||
643 | char = keypath[index]; | ||
644 | if (__indexOf.call(interfaces, char) >= 0) { | ||
645 | tokens.push(current); | ||
646 | current = { | ||
647 | "interface": char, | ||
648 | path: '' | ||
649 | }; | ||
650 | } else { | ||
651 | current.path += char; | ||
560 | } | 652 | } |
561 | _ref = this.bindings; | ||
562 | _results = []; | ||
563 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
564 | binding = _ref[_i]; | ||
565 | _results.push(binding.update(models)); | ||
566 | } | 653 | } |
567 | return _results; | 654 | tokens.push(current); |
655 | return tokens; | ||
568 | }; | 656 | }; |
569 | 657 | ||
570 | return View; | 658 | return KeypathParser; |
571 | 659 | ||
572 | })(); | 660 | })(); |
573 | 661 | ||
... | @@ -630,76 +718,86 @@ | ... | @@ -630,76 +718,86 @@ |
630 | 718 | ||
631 | })(); | 719 | })(); |
632 | 720 | ||
633 | Rivets.Util = { | 721 | KeypathObserver = (function() { |
634 | bindEvent: function(el, event, handler) { | 722 | function KeypathObserver(view, model, keypath, callback) { |
635 | if (window.jQuery != null) { | 723 | this.view = view; |
636 | el = jQuery(el); | 724 | this.model = model; |
637 | if (el.on != null) { | 725 | this.keypath = keypath; |
638 | return el.on(event, handler); | 726 | this.callback = callback; |
639 | } else { | 727 | this.realize = __bind(this.realize, this); |
640 | return el.bind(event, handler); | 728 | this.update = __bind(this.update, this); |
729 | this.parse = __bind(this.parse, this); | ||
730 | this.parse(); | ||
731 | this.objectPath = []; | ||
732 | this.target = this.realize(); | ||
641 | } | 733 | } |
642 | } else if (window.addEventListener != null) { | 734 | |
643 | return el.addEventListener(event, handler, false); | 735 | KeypathObserver.prototype.parse = function() { |
644 | } else { | 736 | var interfaces, k, path, root, v, _ref; |
645 | event = 'on' + event; | 737 | interfaces = (function() { |
646 | return el.attachEvent(event, handler); | 738 | var _ref, _results; |
739 | _ref = this.view.adapters; | ||
740 | _results = []; | ||
741 | for (k in _ref) { | ||
742 | v = _ref[k]; | ||
743 | _results.push(k); | ||
647 | } | 744 | } |
648 | }, | 745 | return _results; |
649 | unbindEvent: function(el, event, handler) { | 746 | }).call(this); |
650 | if (window.jQuery != null) { | 747 | if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { |
651 | el = jQuery(el); | 748 | root = this.keypath[0]; |
652 | if (el.off != null) { | 749 | path = this.keypath.substr(1); |
653 | return el.off(event, handler); | ||
654 | } else { | 750 | } else { |
655 | return el.unbind(event, handler); | 751 | root = this.view.config.rootInterface; |
752 | path = this.keypath; | ||
656 | } | 753 | } |
657 | } else if (window.removeEventListener != null) { | 754 | this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); |
658 | return el.removeEventListener(event, handler, false); | 755 | return this.key = this.tokens.pop(); |
659 | } else { | 756 | }; |
660 | event = 'on' + event; | 757 | |
661 | return el.detachEvent(event, handler); | 758 | KeypathObserver.prototype.update = function() { |
759 | var next, prev; | ||
760 | if ((next = this.realize()) !== this.target) { | ||
761 | prev = this.target; | ||
762 | this.target = next; | ||
763 | return this.callback(this, prev); | ||
662 | } | 764 | } |
663 | }, | 765 | }; |
664 | getInputValue: function(el) { | 766 | |
665 | var o, _i, _len, _results; | 767 | KeypathObserver.prototype.realize = function() { |
666 | if (window.jQuery != null) { | 768 | var current, index, prev, token, _i, _len, _ref; |
667 | el = jQuery(el); | 769 | current = this.model; |
668 | switch (el[0].type) { | 770 | _ref = this.tokens; |
669 | case 'checkbox': | 771 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { |
670 | return el.is(':checked'); | 772 | token = _ref[index]; |
671 | default: | 773 | if (this.objectPath[index] != null) { |
672 | return el.val(); | 774 | if (current !== (prev = this.objectPath[index])) { |
775 | this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); | ||
776 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); | ||
777 | this.objectPath[index] = current; | ||
673 | } | 778 | } |
674 | } else { | 779 | } else { |
675 | switch (el.type) { | 780 | this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); |
676 | case 'checkbox': | 781 | this.objectPath[index] = current; |
677 | return el.checked; | ||
678 | case 'select-multiple': | ||
679 | _results = []; | ||
680 | for (_i = 0, _len = el.length; _i < _len; _i++) { | ||
681 | o = el[_i]; | ||
682 | if (o.selected) { | ||
683 | _results.push(o.value); | ||
684 | } | ||
685 | } | ||
686 | return _results; | ||
687 | break; | ||
688 | default: | ||
689 | return el.value; | ||
690 | } | ||
691 | } | 782 | } |
783 | current = this.view.adapters[token["interface"]].read(current, token.path); | ||
692 | } | 784 | } |
785 | return current; | ||
693 | }; | 786 | }; |
694 | 787 | ||
695 | Rivets.binders = { | 788 | return KeypathObserver; |
696 | enabled: function(el, value) { | 789 | |
790 | })(); | ||
791 | |||
792 | Rivets.binders.enabled = function(el, value) { | ||
697 | return el.disabled = !value; | 793 | return el.disabled = !value; |
698 | }, | 794 | }; |
699 | disabled: function(el, value) { | 795 | |
796 | Rivets.binders.disabled = function(el, value) { | ||
700 | return el.disabled = !!value; | 797 | return el.disabled = !!value; |
701 | }, | 798 | }; |
702 | checked: { | 799 | |
800 | Rivets.binders.checked = { | ||
703 | publishes: true, | 801 | publishes: true, |
704 | bind: function(el) { | 802 | bind: function(el) { |
705 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 803 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
... | @@ -715,8 +813,9 @@ | ... | @@ -715,8 +813,9 @@ |
715 | return el.checked = !!value; | 813 | return el.checked = !!value; |
716 | } | 814 | } |
717 | } | 815 | } |
718 | }, | 816 | }; |
719 | unchecked: { | 817 | |
818 | Rivets.binders.unchecked = { | ||
720 | publishes: true, | 819 | publishes: true, |
721 | bind: function(el) { | 820 | bind: function(el) { |
722 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 821 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
... | @@ -732,17 +831,21 @@ | ... | @@ -732,17 +831,21 @@ |
732 | return el.checked = !value; | 831 | return el.checked = !value; |
733 | } | 832 | } |
734 | } | 833 | } |
735 | }, | 834 | }; |
736 | show: function(el, value) { | 835 | |
836 | Rivets.binders.show = function(el, value) { | ||
737 | return el.style.display = value ? '' : 'none'; | 837 | return el.style.display = value ? '' : 'none'; |
738 | }, | 838 | }; |
739 | hide: function(el, value) { | 839 | |
840 | Rivets.binders.hide = function(el, value) { | ||
740 | return el.style.display = value ? 'none' : ''; | 841 | return el.style.display = value ? 'none' : ''; |
741 | }, | 842 | }; |
742 | html: function(el, value) { | 843 | |
844 | Rivets.binders.html = function(el, value) { | ||
743 | return el.innerHTML = value != null ? value : ''; | 845 | return el.innerHTML = value != null ? value : ''; |
744 | }, | 846 | }; |
745 | value: { | 847 | |
848 | Rivets.binders.value = { | ||
746 | publishes: true, | 849 | publishes: true, |
747 | bind: function(el) { | 850 | bind: function(el) { |
748 | return Rivets.Util.bindEvent(el, 'change', this.publish); | 851 | return Rivets.Util.bindEvent(el, 'change', this.publish); |
... | @@ -772,20 +875,22 @@ | ... | @@ -772,20 +875,22 @@ |
772 | } | 875 | } |
773 | } | 876 | } |
774 | } | 877 | } |
775 | }, | 878 | }; |
776 | text: function(el, value) { | 879 | |
880 | Rivets.binders.text = function(el, value) { | ||
777 | if (el.innerText != null) { | 881 | if (el.innerText != null) { |
778 | return el.innerText = value != null ? value : ''; | 882 | return el.innerText = value != null ? value : ''; |
779 | } else { | 883 | } else { |
780 | return el.textContent = value != null ? value : ''; | 884 | return el.textContent = value != null ? value : ''; |
781 | } | 885 | } |
782 | }, | 886 | }; |
783 | "if": { | 887 | |
888 | Rivets.binders["if"] = { | ||
784 | block: true, | 889 | block: true, |
785 | bind: function(el) { | 890 | bind: function(el) { |
786 | var attr, declaration; | 891 | var attr, declaration; |
787 | if (this.marker == null) { | 892 | if (this.marker == null) { |
788 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); | 893 | attr = [this.view.config.prefix, this.type].join('-').replace('--', '-'); |
789 | declaration = el.getAttribute(attr); | 894 | declaration = el.getAttribute(attr); |
790 | this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " "); | 895 | this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " "); |
791 | el.removeAttribute(attr); | 896 | el.removeAttribute(attr); |
... | @@ -810,6 +915,7 @@ | ... | @@ -810,6 +915,7 @@ |
810 | options = { | 915 | options = { |
811 | binders: this.view.options.binders, | 916 | binders: this.view.options.binders, |
812 | formatters: this.view.options.formatters, | 917 | formatters: this.view.options.formatters, |
918 | adapters: this.view.options.adapters, | ||
813 | config: this.view.options.config | 919 | config: this.view.options.config |
814 | }; | 920 | }; |
815 | (this.nested = new Rivets.View(el, models, options)).bind(); | 921 | (this.nested = new Rivets.View(el, models, options)).bind(); |
... | @@ -825,8 +931,9 @@ | ... | @@ -825,8 +931,9 @@ |
825 | var _ref; | 931 | var _ref; |
826 | return (_ref = this.nested) != null ? _ref.update(models) : void 0; | 932 | return (_ref = this.nested) != null ? _ref.update(models) : void 0; |
827 | } | 933 | } |
828 | }, | 934 | }; |
829 | unless: { | 935 | |
936 | Rivets.binders.unless = { | ||
830 | block: true, | 937 | block: true, |
831 | bind: function(el) { | 938 | bind: function(el) { |
832 | return Rivets.binders["if"].bind.call(this, el); | 939 | return Rivets.binders["if"].bind.call(this, el); |
... | @@ -840,8 +947,9 @@ | ... | @@ -840,8 +947,9 @@ |
840 | update: function(models) { | 947 | update: function(models) { |
841 | return Rivets.binders["if"].update.call(this, models); | 948 | return Rivets.binders["if"].update.call(this, models); |
842 | } | 949 | } |
843 | }, | 950 | }; |
844 | "on-*": { | 951 | |
952 | Rivets.binders['on-*'] = { | ||
845 | "function": true, | 953 | "function": true, |
846 | unbind: function(el) { | 954 | unbind: function(el) { |
847 | if (this.handler) { | 955 | if (this.handler) { |
... | @@ -854,13 +962,14 @@ | ... | @@ -854,13 +962,14 @@ |
854 | } | 962 | } |
855 | return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value)); | 963 | return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value)); |
856 | } | 964 | } |
857 | }, | 965 | }; |
858 | "each-*": { | 966 | |
967 | Rivets.binders['each-*'] = { | ||
859 | block: true, | 968 | block: true, |
860 | bind: function(el) { | 969 | bind: function(el) { |
861 | var attr; | 970 | var attr; |
862 | if (this.marker == null) { | 971 | if (this.marker == null) { |
863 | attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-'); | 972 | attr = [this.view.config.prefix, this.type].join('-').replace('--', '-'); |
864 | this.marker = document.createComment(" rivets: " + this.type + " "); | 973 | this.marker = document.createComment(" rivets: " + this.type + " "); |
865 | this.iterated = []; | 974 | this.iterated = []; |
866 | el.removeAttribute(attr); | 975 | el.removeAttribute(attr); |
... | @@ -881,7 +990,7 @@ | ... | @@ -881,7 +990,7 @@ |
881 | } | 990 | } |
882 | }, | 991 | }, |
883 | routine: function(el, collection) { | 992 | routine: function(el, collection) { |
884 | var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results; | 993 | 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; |
885 | modelName = this.args[0]; | 994 | modelName = this.args[0]; |
886 | collection = collection || []; | 995 | collection = collection || []; |
887 | if (this.iterated.length > collection.length) { | 996 | if (this.iterated.length > collection.length) { |
... | @@ -893,7 +1002,6 @@ | ... | @@ -893,7 +1002,6 @@ |
893 | this.marker.parentNode.removeChild(view.els[0]); | 1002 | this.marker.parentNode.removeChild(view.els[0]); |
894 | } | 1003 | } |
895 | } | 1004 | } |
896 | _results = []; | ||
897 | for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { | 1005 | for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { |
898 | model = collection[index]; | 1006 | model = collection[index]; |
899 | data = {}; | 1007 | data = {}; |
... | @@ -910,6 +1018,7 @@ | ... | @@ -910,6 +1018,7 @@ |
910 | options = { | 1018 | options = { |
911 | binders: this.view.options.binders, | 1019 | binders: this.view.options.binders, |
912 | formatters: this.view.options.formatters, | 1020 | formatters: this.view.options.formatters, |
1021 | adapters: this.view.options.adapters, | ||
913 | config: {} | 1022 | config: {} |
914 | }; | 1023 | }; |
915 | _ref2 = this.view.options.config; | 1024 | _ref2 = this.view.options.config; |
... | @@ -922,14 +1031,24 @@ | ... | @@ -922,14 +1031,24 @@ |
922 | view = new Rivets.View(template, data, options); | 1031 | view = new Rivets.View(template, data, options); |
923 | view.bind(); | 1032 | view.bind(); |
924 | this.iterated.push(view); | 1033 | this.iterated.push(view); |
925 | _results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling)); | 1034 | this.marker.parentNode.insertBefore(template, previous.nextSibling); |
926 | } else if (this.iterated[index].models[modelName] !== model) { | 1035 | } else if (this.iterated[index].models[modelName] !== model) { |
927 | _results.push(this.iterated[index].update(data)); | 1036 | this.iterated[index].update(data); |
1037 | } | ||
1038 | } | ||
1039 | if (el.nodeName === 'OPTION') { | ||
1040 | _ref3 = this.view.bindings; | ||
1041 | _results = []; | ||
1042 | for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { | ||
1043 | binding = _ref3[_k]; | ||
1044 | if (binding.el === this.marker.parentNode && binding.type === 'value') { | ||
1045 | _results.push(binding.sync()); | ||
928 | } else { | 1046 | } else { |
929 | _results.push(void 0); | 1047 | _results.push(void 0); |
930 | } | 1048 | } |
931 | } | 1049 | } |
932 | return _results; | 1050 | return _results; |
1051 | } | ||
933 | }, | 1052 | }, |
934 | update: function(models) { | 1053 | update: function(models) { |
935 | var data, key, model, view, _i, _len, _ref, _results; | 1054 | var data, key, model, view, _i, _len, _ref, _results; |
... | @@ -948,39 +1067,139 @@ | ... | @@ -948,39 +1067,139 @@ |
948 | } | 1067 | } |
949 | return _results; | 1068 | return _results; |
950 | } | 1069 | } |
951 | }, | 1070 | }; |
952 | "class-*": function(el, value) { | 1071 | |
1072 | Rivets.binders['class-*'] = function(el, value) { | ||
953 | var elClass; | 1073 | var elClass; |
954 | elClass = " " + el.className + " "; | 1074 | elClass = " " + el.className + " "; |
955 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { | 1075 | if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) { |
956 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); | 1076 | return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim(); |
957 | } | 1077 | } |
958 | }, | 1078 | }; |
959 | "*": function(el, value) { | 1079 | |
1080 | Rivets.binders['*'] = function(el, value) { | ||
960 | if (value) { | 1081 | if (value) { |
961 | return el.setAttribute(this.type, value); | 1082 | return el.setAttribute(this.type, value); |
962 | } else { | 1083 | } else { |
963 | return el.removeAttribute(this.type); | 1084 | return el.removeAttribute(this.type); |
964 | } | 1085 | } |
965 | } | ||
966 | }; | 1086 | }; |
967 | 1087 | ||
968 | Rivets.components = {}; | 1088 | Rivets.adapters['.'] = { |
969 | 1089 | id: '_rv', | |
970 | Rivets.config = { | 1090 | counter: 0, |
971 | preloadData: true, | 1091 | weakmap: {}, |
972 | handler: function(context, ev, binding) { | 1092 | weakReference: function(obj) { |
973 | return this.call(context, ev, binding.view.models); | 1093 | var id; |
1094 | if (obj[this.id] == null) { | ||
1095 | id = this.counter++; | ||
1096 | this.weakmap[id] = { | ||
1097 | callbacks: {} | ||
1098 | }; | ||
1099 | Object.defineProperty(obj, this.id, { | ||
1100 | value: id | ||
1101 | }); | ||
1102 | } | ||
1103 | return this.weakmap[obj[this.id]]; | ||
1104 | }, | ||
1105 | stubFunction: function(obj, fn) { | ||
1106 | var map, original, weakmap; | ||
1107 | original = obj[fn]; | ||
1108 | map = this.weakReference(obj); | ||
1109 | weakmap = this.weakmap; | ||
1110 | return obj[fn] = function() { | ||
1111 | var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; | ||
1112 | response = original.apply(obj, arguments); | ||
1113 | _ref = map.pointers; | ||
1114 | for (r in _ref) { | ||
1115 | k = _ref[r]; | ||
1116 | _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; | ||
1117 | for (_i = 0, _len = _ref3.length; _i < _len; _i++) { | ||
1118 | callback = _ref3[_i]; | ||
1119 | callback(); | ||
1120 | } | ||
1121 | } | ||
1122 | return response; | ||
1123 | }; | ||
1124 | }, | ||
1125 | observeMutations: function(obj, ref, keypath) { | ||
1126 | var fn, functions, map, _base, _i, _len; | ||
1127 | if (Array.isArray(obj)) { | ||
1128 | map = this.weakReference(obj); | ||
1129 | if (map.pointers == null) { | ||
1130 | map.pointers = {}; | ||
1131 | functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; | ||
1132 | for (_i = 0, _len = functions.length; _i < _len; _i++) { | ||
1133 | fn = functions[_i]; | ||
1134 | this.stubFunction(obj, fn); | ||
1135 | } | ||
1136 | } | ||
1137 | if ((_base = map.pointers)[ref] == null) { | ||
1138 | _base[ref] = []; | ||
1139 | } | ||
1140 | if (__indexOf.call(map.pointers[ref], keypath) < 0) { | ||
1141 | return map.pointers[ref].push(keypath); | ||
1142 | } | ||
1143 | } | ||
1144 | }, | ||
1145 | unobserveMutations: function(obj, ref, keypath) { | ||
1146 | var keypaths, _ref; | ||
1147 | if (Array.isArray(obj && (obj[this.id] != null))) { | ||
1148 | if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { | ||
1149 | return keypaths.splice(keypaths.indexOf(keypath), 1); | ||
1150 | } | ||
1151 | } | ||
1152 | }, | ||
1153 | subscribe: function(obj, keypath, callback) { | ||
1154 | var callbacks, value, | ||
1155 | _this = this; | ||
1156 | callbacks = this.weakReference(obj).callbacks; | ||
1157 | if (callbacks[keypath] == null) { | ||
1158 | callbacks[keypath] = []; | ||
1159 | value = obj[keypath]; | ||
1160 | Object.defineProperty(obj, keypath, { | ||
1161 | get: function() { | ||
1162 | return value; | ||
1163 | }, | ||
1164 | set: function(newValue) { | ||
1165 | var _i, _len, _ref; | ||
1166 | if (newValue !== value) { | ||
1167 | value = newValue; | ||
1168 | _ref = callbacks[keypath]; | ||
1169 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
1170 | callback = _ref[_i]; | ||
1171 | callback(); | ||
1172 | } | ||
1173 | return _this.observeMutations(newValue, obj[_this.id], keypath); | ||
1174 | } | ||
1175 | } | ||
1176 | }); | ||
1177 | } | ||
1178 | if (__indexOf.call(callbacks[keypath], callback) < 0) { | ||
1179 | callbacks[keypath].push(callback); | ||
1180 | } | ||
1181 | return this.observeMutations(obj[keypath], obj[this.id], keypath); | ||
1182 | }, | ||
1183 | unsubscribe: function(obj, keypath, callback) { | ||
1184 | var callbacks; | ||
1185 | callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; | ||
1186 | callbacks.splice(callbacks.indexOf(callback), 1); | ||
1187 | return this.unobserveMutations(obj[keypath], obj[this.id], keypath); | ||
1188 | }, | ||
1189 | read: function(obj, keypath) { | ||
1190 | return obj[keypath]; | ||
1191 | }, | ||
1192 | publish: function(obj, keypath, value) { | ||
1193 | return obj[keypath] = value; | ||
974 | } | 1194 | } |
975 | }; | 1195 | }; |
976 | |||
977 | Rivets.formatters = {}; | ||
978 | 1196 | ||
979 | Rivets.factory = function(exports) { | 1197 | Rivets.factory = function(exports) { |
980 | exports._ = Rivets; | 1198 | exports._ = Rivets; |
981 | exports.binders = Rivets.binders; | 1199 | exports.binders = Rivets.binders; |
982 | exports.components = Rivets.components; | 1200 | exports.components = Rivets.components; |
983 | exports.formatters = Rivets.formatters; | 1201 | exports.formatters = Rivets.formatters; |
1202 | exports.adapters = Rivets.adapters; | ||
984 | exports.config = Rivets.config; | 1203 | exports.config = Rivets.config; |
985 | exports.configure = function(options) { | 1204 | exports.configure = function(options) { |
986 | var property, value; | 1205 | var property, value; | ... | ... |
1 | // Rivets.js | 1 | // Rivets.js |
2 | // version: 0.5.13 | 2 | // version: 0.6.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | !function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},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},g=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};a={},b=window.jQuery||window.Zepto,String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function b(a,b,d,e,f,g){var h,i,j,k;if(this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),!(this.binder=this.view.binders[d])){k=this.view.binders;for(h in k)j=k[h],"*"!==h&&-1!==h.indexOf("*")&&(i=new RegExp("^"+h.replace("*",".+")+"$"),i.test(d)&&(this.binder=j,this.args=new RegExp("^"+h.replace("*","(.+)")+"$").exec(d),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return b.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.model[e]instanceof Function?this.model[e]: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},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(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},b.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.config.adapter.publish(this.model,this.keypath,f)},b.prototype.bind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.subscribe(c,b,this.sync));return i}},b.prototype.unbind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.unsubscribe(c,b,this.sync));return i}},b.prototype.update=function(a){var b;return null==a&&(a={}),this.key?a[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=a[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function d(b,d,e){var f,h,i,j,k;for(this.view=b,this.el=d,this.type=e,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(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++)f=j[h],k=f.name,g.call(this.component.attributes,k)>=0?this.attributes[f.name]=f.value:this.inflections[f.name]=f.value}return f(d,b),d.prototype.sync=function(){},d.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},d.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},d.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))},d.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},d}(a.Binding),a.TextBinding=function(a){function b(a,b,d,e,f,g){this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return f(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.View=function(){function b(b,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),"undefined"==typeof this.els.length&&(this.els=[this.els]),l=["config","binders","formatters"],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(){var a;return a=this.config.prefix,a?new RegExp("^data-"+a+"-"):/^data-/},b.prototype.componentRegExp=function(){var a,b;return new RegExp("^"+(null!=(a=null!=(b=this.config.prefix)?b.toUpperCase():void 0)?a:"RV")+"-")},b.prototype.build=function(){var b,c,e,f,h,i,j,k,l,m=this;for(this.bindings=[],i=[],b=this.bindingRegExp(),e=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,l,n,o,p;return k={},o=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)n=c[a],d.push(n.trim());return d}(),f=function(){var a,b,c,d;for(c=o.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),l=f.shift(),p=l.split(/\.|:/),k.formatters=o,k.bypass=-1!==l.indexOf(":"),p[0]?i=p.shift():(i=null,p.shift()),j=p.join("."),(h=f.shift())&&(k.dependencies=h.split(/\s+/)),m.bindings.push(new a[b](m,c,d,i,j,k))},h=function(f){var j,k,l,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,N,O,P;if(g.call(i,f)<0){if(f.nodeType===Node.TEXT_NODE){if(r=a.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(f.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?d.call(x,1):[],f.data=u.value,0===u.type?f.data=u.value:c("TextBinding",f,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),f.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(e.test(f.tagName))y=f.tagName.replace(e,"").toLowerCase(),m.bindings.push(new a.ComponentBinding(m,f,y));else if(null!=f.attributes){for(K=f.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],b.test(j.name)){if(y=j.name.replace(b,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=f.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||f.attributes,D=0,H=N.length;H>D;D++)j=N[D],b.test(j.name)&&(y=j.name.replace(b,""),c("Binding",f,y,j.value))}for(O=f.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)f=l[j],h(f)},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.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+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),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+2}return h},a}(),a.Util={bindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.on?a.on(c,d):a.bind(c,d)):null!=window.addEventListener?a.addEventListener(c,d,!1):(c="on"+c,a.attachEvent(c,d))},unbindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.off?a.off(c,d):a.unbind(c,d)):null!=window.removeEventListener?a.removeEventListener(c,d,!1):(c="on"+c,a.detachEvent(c,d))},getInputValue:function(a){var c,d,e,f;if(null!=window.jQuery)switch(a=b(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(f=[],d=0,e=a.length;e>d;d++)c=a[d],c.selected&&f.push(c.value);return f;default:return a.value}}},a.binders={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},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}},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}},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=null!=b?b:""},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,c){var d,e,f,h,i,j,k;if(null!=window.jQuery){if(a=b(a),(null!=c?c.toString():void 0)!==(null!=(h=a.val())?h.toString():void 0))return a.val(null!=c?c:"")}else if("select-multiple"===a.type){if(null!=c){for(k=[],e=0,f=a.length;f>e;e++)d=a[e],k.push(d.selected=(i=d.value,g.call(c,i)>=0));return k}}else if((null!=c?c.toString():void 0)!==(null!=(j=a.value)?j.toString():void 0))return a.value=null!=c?c:""}},text:function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},"if":{block:!0,bind:function(a){var b,c;return null==this.marker?(b=["data",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,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}},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)}},"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))}},"each-*":{block:!0,bind:function(a){var b;return null==this.marker?(b=["data",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;if(j=this.args[0],c=c||[],this.iterated.length>c.length)for(t=Array(this.iterated.length-c.length),p=0,r=t.length;r>p;p++)e=t[p],o=this.iterated.pop(),o.unbind(),this.marker.parentNode.removeChild(o.els[0]);for(w=[],f=q=0,s=c.length;s>q;f=++q)if(i=c[f],d={},d[j]=i,null==this.iterated[f]){u=this.view.models;for(h in u)i=u[h],null==d[h]&&(d[h]=i);l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,k={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},v=this.view.options.config;for(g in v)n=v[g],k.config[g]=n;k.config.preloadData=!0,m=b.cloneNode(!0),o=new a.View(m,d,k),o.bind(),this.iterated.push(o),w.push(this.marker.parentNode.insertBefore(m,l.nextSibling))}else this.iterated[f].models[j]!==i?w.push(this.iterated[f].update(d)):w.push(void 0);return w},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}},"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},"*":function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)}},a.components={},a.config={preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}},a.formatters={},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,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=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.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}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(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=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,f,g,h,i,j,k,l,m=this;for(this.bindings=[],i=[],a=this.bindingRegExp(),f=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,l;return j={},l=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=l.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=l,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),m.bindings.push(new b[a](m,c,d,i,j))},h=function(g){var j,k,l,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,N,O,P;if(d.call(i,g)<0){if(g.nodeType===Node.TEXT_NODE){if(r=b.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(g.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?e.call(x,1):[],g.data=u.value,0===u.type?g.data=u.value:c("TextBinding",g,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),g.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(f.test(g.tagName))y=g.tagName.replace(f,"").toLowerCase(),m.bindings.push(new b.ComponentBinding(m,g,y));else if(null!=g.attributes){for(K=g.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],a.test(j.name)){if(y=j.name.replace(a,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=g.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||g.attributes,D=0,H=N.length;H>D;D++)j=N[D],a.test(j.name)&&(y=j.name.replace(a,""),c("Binding",g,y,j.value))}for(O=g.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)g=l[j],h(g)},a.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},a.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},a.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},a.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},a.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},a.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},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.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},d.prototype.setObserver=function(){var b=this;return this.observer=new a(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},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],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(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.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},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.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(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}},d.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.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=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.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,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),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 a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(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}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.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+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),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+2}return h},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=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],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.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},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"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}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"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}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,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++)c=a[e],j.push(c.selected=(h=c.value,d.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:""}},b.binders.text=function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},b.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(a,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 b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.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(a,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=a.cloneNode(!0),p=new b.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"===a.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}},b.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},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.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 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]=[]),d.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 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(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[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}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.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 facility.", | 3 | "description": "Declarative data binding + templating solution.", |
4 | "version": "0.5.13", | 4 | "version": "0.6.0", |
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