ende 0.3.5 → 0.3.6

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