rivets-rails 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
- TAG=v0.5.10
1
+ TAG=v0.5.12
2
2
  curl -s -o dist/rivets.js https://raw.github.com/mikeric/rivets/$TAG/dist/rivets.js
3
3
  curl -s -o dist/rivets.min.js https://raw.github.com/mikeric/rivets/$TAG/dist/rivets.min.js
@@ -19,4 +19,8 @@
19
19
  }
20
20
  };
21
21
 
22
+ rivets.binders.color = function(el, value) {
23
+ return el.style.color = value;
24
+ };
25
+
22
26
  }).call(this);
@@ -51,4 +51,13 @@
51
51
  return value.length;
52
52
  };
53
53
 
54
+ rivets.formatters.mask = function(value, left, right, mask) {
55
+ var formatted;
56
+
57
+ formatted = value.substring(0, left);
58
+ formatted + mask;
59
+ formatted += value.substring(value.length - right);
60
+ return formatted;
61
+ };
62
+
54
63
  }).call(this);
@@ -34,6 +34,10 @@
34
34
  }
35
35
  };
36
36
 
37
+ rivets.binders.color = function(el, value) {
38
+ return el.style.color = value;
39
+ };
40
+
37
41
  }).call(this);
38
42
 
39
43
  (function() {
@@ -89,6 +93,15 @@
89
93
  return value.length;
90
94
  };
91
95
 
96
+ rivets.formatters.mask = function(value, left, right, mask) {
97
+ var formatted;
98
+
99
+ formatted = value.substring(0, left);
100
+ formatted + mask;
101
+ formatted += value.substring(value.length - right);
102
+ return formatted;
103
+ };
104
+
92
105
  }).call(this);
93
106
 
94
107
  (function() {
@@ -2,4 +2,4 @@
2
2
  // version: 0.1.1
3
3
  // author: vkill
4
4
  // license: MIT
5
- !function(){var a;a=this.rivets,a.configure({prefix:"bind"})}.call(this),function(){var a;a=this.rivets,a.binders.content={routine:a.binders.text,bind:function(b){var c;return c=this,this.callback=function(){return a.config.adapter.publish(c.model,c.keypath,b.textContent)},b.addEventListener("input",this.callback,!1)},unbind:function(a){return a.removeEventListener("input",this.callback,!1)}}}.call(this),function(){var a;a=this.rivets,a.formatters.shortdate=function(a){return moment(a).format("YYYY-MM-DD")},a.formatters.number=function(a){return+a},a.formatters.string=function(a){return String(a)},a.formatters.negate=function(a){return!a},a.formatters.eq=function(a,b){return a===b},a.formatters.not_eq=function(a,b){return a!==b},a.formatters.gt=function(a,b){return a>b},a.formatters.lt=function(a,b){return b>a},a.formatters.blank=function(a){return null==a||""===a},a.formatters.preventDefault=function(a){return function(b){return b.preventDefault(),a.call(this,b),!1}},a.formatters.length=function(a){return a.length}}.call(this),function(){var a;a=this.rivets,a.configure({adapter:{subscribe:function(a,b,c){return null!=a.on?a.on("change:"+b,c):void 0},unsubscribe:function(a,b,c){return null!=a.off?a.off("change:"+b,c):void 0},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}}})}.call(this);
5
+ !function(){var a;a=this.rivets,a.configure({prefix:"bind"})}.call(this),function(){var a;a=this.rivets,a.binders.content={routine:a.binders.text,bind:function(b){var c;return c=this,this.callback=function(){return a.config.adapter.publish(c.model,c.keypath,b.textContent)},b.addEventListener("input",this.callback,!1)},unbind:function(a){return a.removeEventListener("input",this.callback,!1)}},a.binders.color=function(a,b){return a.style.color=b}}.call(this),function(){var a;a=this.rivets,a.formatters.shortdate=function(a){return moment(a).format("YYYY-MM-DD")},a.formatters.number=function(a){return+a},a.formatters.string=function(a){return String(a)},a.formatters.negate=function(a){return!a},a.formatters.eq=function(a,b){return a===b},a.formatters.not_eq=function(a,b){return a!==b},a.formatters.gt=function(a,b){return a>b},a.formatters.lt=function(a,b){return b>a},a.formatters.blank=function(a){return null==a||""===a},a.formatters.preventDefault=function(a){return function(b){return b.preventDefault(),a.call(this,b),!1}},a.formatters.length=function(a){return a.length},a.formatters.mask=function(a,b,c,d){var e;return e=a.substring(0,b),e+=a.substring(a.length-c)}}.call(this),function(){var a;a=this.rivets,a.configure({adapter:{subscribe:function(a,b,c){return null!=a.on?a.on("change:"+b,c):void 0},unsubscribe:function(a,b,c){return null!=a.off?a.off("change:"+b,c):void 0},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}}})}.call(this);
@@ -1,5 +1,5 @@
1
1
  // Rivets.js
2
- // version: 0.5.10
2
+ // version: 0.5.12
3
3
  // author: Michael Richards
4
4
  // license: MIT
5
5
  (function() {
@@ -21,7 +21,6 @@
21
21
  Rivets.Binding = (function() {
22
22
  function Binding(view, el, type, key, keypath, options) {
23
23
  var identifier, regexp, value, _ref;
24
-
25
24
  this.view = view;
26
25
  this.el = el;
27
26
  this.type = type;
@@ -36,7 +35,7 @@
36
35
  this.set = __bind(this.set, this);
37
36
  this.eventHandler = __bind(this.eventHandler, this);
38
37
  this.formattedValue = __bind(this.formattedValue, this);
39
- if (!(this.binder = Rivets.internalBinders[this.type] || this.view.binders[type])) {
38
+ if (!(this.binder = this.view.binders[type])) {
40
39
  _ref = this.view.binders;
41
40
  for (identifier in _ref) {
42
41
  value = _ref[identifier];
@@ -62,7 +61,6 @@
62
61
 
63
62
  Binding.prototype.formattedValue = function(value) {
64
63
  var args, formatter, id, _i, _len, _ref;
65
-
66
64
  _ref = this.formatters;
67
65
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
68
66
  formatter = _ref[_i];
@@ -80,7 +78,6 @@
80
78
 
81
79
  Binding.prototype.eventHandler = function(fn) {
82
80
  var binding, handler;
83
-
84
81
  handler = (binding = this).view.config.handler;
85
82
  return function(ev) {
86
83
  return handler.call(fn, this, ev, binding);
@@ -89,7 +86,6 @@
89
86
 
90
87
  Binding.prototype.set = function(value) {
91
88
  var _ref;
92
-
93
89
  value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value);
94
90
  return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0;
95
91
  };
@@ -100,7 +96,6 @@
100
96
 
101
97
  Binding.prototype.publish = function() {
102
98
  var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
103
-
104
99
  value = Rivets.Util.getInputValue(this.el);
105
100
  _ref = this.formatters.slice(0).reverse();
106
101
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -116,7 +111,6 @@
116
111
 
117
112
  Binding.prototype.bind = function() {
118
113
  var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
119
-
120
114
  if ((_ref = this.binder.bind) != null) {
121
115
  _ref.call(this, this.el);
122
116
  }
@@ -149,7 +143,6 @@
149
143
 
150
144
  Binding.prototype.unbind = function() {
151
145
  var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
152
-
153
146
  if ((_ref = this.binder.unbind) != null) {
154
147
  _ref.call(this, this.el);
155
148
  }
@@ -177,7 +170,6 @@
177
170
 
178
171
  Binding.prototype.update = function(models) {
179
172
  var _ref;
180
-
181
173
  if (models == null) {
182
174
  models = {};
183
175
  }
@@ -211,7 +203,6 @@
211
203
 
212
204
  function ComponentBinding(view, el, type) {
213
205
  var attribute, _i, _len, _ref, _ref1;
214
-
215
206
  this.view = view;
216
207
  this.el = el;
217
208
  this.type = type;
@@ -236,8 +227,7 @@
236
227
  ComponentBinding.prototype.sync = function() {};
237
228
 
238
229
  ComponentBinding.prototype.locals = function(models) {
239
- var inverse, key, model, result, _ref, _ref1;
240
-
230
+ var inverse, key, model, path, result, _i, _len, _ref, _ref1;
241
231
  if (models == null) {
242
232
  models = this.view.models;
243
233
  }
@@ -245,11 +235,15 @@
245
235
  _ref = this.inflections;
246
236
  for (key in _ref) {
247
237
  inverse = _ref[key];
248
- result[key] = models[inverse];
238
+ _ref1 = inverse.split('.');
239
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
240
+ path = _ref1[_i];
241
+ result[key] = (result[key] || models)[path];
242
+ }
249
243
  }
250
244
  for (key in models) {
251
245
  model = models[key];
252
- if ((_ref1 = result[key]) == null) {
246
+ if (result[key] == null) {
253
247
  result[key] = model;
254
248
  }
255
249
  }
@@ -258,13 +252,11 @@
258
252
 
259
253
  ComponentBinding.prototype.update = function(models) {
260
254
  var _ref;
261
-
262
255
  return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0;
263
256
  };
264
257
 
265
258
  ComponentBinding.prototype.bind = function() {
266
259
  var el, _ref;
267
-
268
260
  if (this.componentView != null) {
269
261
  return (_ref = this.componentView) != null ? _ref.bind() : void 0;
270
262
  } else {
@@ -276,7 +268,6 @@
276
268
 
277
269
  ComponentBinding.prototype.unbind = function() {
278
270
  var _ref;
279
-
280
271
  return (_ref = this.componentView) != null ? _ref.unbind() : void 0;
281
272
  };
282
273
 
@@ -284,10 +275,38 @@
284
275
 
285
276
  })(Rivets.Binding);
286
277
 
278
+ Rivets.TextBinding = (function(_super) {
279
+ __extends(TextBinding, _super);
280
+
281
+ function TextBinding(view, el, type, key, keypath, options) {
282
+ this.view = view;
283
+ this.el = el;
284
+ this.type = type;
285
+ this.key = key;
286
+ this.keypath = keypath;
287
+ this.options = options != null ? options : {};
288
+ this.sync = __bind(this.sync, this);
289
+ this.formatters = this.options.formatters || [];
290
+ this.model = this.key ? this.view.models[this.key] : this.view.models;
291
+ }
292
+
293
+ TextBinding.prototype.binder = {
294
+ routine: function(node, value) {
295
+ return node.data = value != null ? value : '';
296
+ }
297
+ };
298
+
299
+ TextBinding.prototype.sync = function() {
300
+ return TextBinding.__super__.sync.apply(this, arguments);
301
+ };
302
+
303
+ return TextBinding;
304
+
305
+ })(Rivets.Binding);
306
+
287
307
  Rivets.View = (function() {
288
308
  function View(els, models, options) {
289
- var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
290
-
309
+ var k, option, v, _base, _i, _len, _ref, _ref1, _ref2;
291
310
  this.els = els;
292
311
  this.models = models;
293
312
  this.options = options != null ? options : {};
@@ -317,7 +336,7 @@
317
336
  _ref2 = Rivets[option];
318
337
  for (k in _ref2) {
319
338
  v = _ref2[k];
320
- if ((_ref3 = (_base = this[option])[k]) == null) {
339
+ if ((_base = this[option])[k] == null) {
321
340
  _base[k] = v;
322
341
  }
323
342
  }
@@ -327,7 +346,6 @@
327
346
 
328
347
  View.prototype.bindingRegExp = function() {
329
348
  var prefix;
330
-
331
349
  prefix = this.config.prefix;
332
350
  if (prefix) {
333
351
  return new RegExp("^data-" + prefix + "-");
@@ -338,25 +356,21 @@
338
356
 
339
357
  View.prototype.componentRegExp = function() {
340
358
  var _ref, _ref1;
341
-
342
359
  return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-");
343
360
  };
344
361
 
345
362
  View.prototype.build = function() {
346
363
  var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref,
347
364
  _this = this;
348
-
349
365
  this.bindings = [];
350
366
  skipNodes = [];
351
367
  bindingRegExp = this.bindingRegExp();
352
368
  componentRegExp = this.componentRegExp();
353
- buildBinding = function(node, type, declaration) {
369
+ buildBinding = function(binding, node, type, declaration) {
354
370
  var context, ctx, dependencies, key, keypath, options, path, pipe, pipes, splitPath;
355
-
356
371
  options = {};
357
372
  pipes = (function() {
358
373
  var _i, _len, _ref, _results;
359
-
360
374
  _ref = declaration.split('|');
361
375
  _results = [];
362
376
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -367,7 +381,6 @@
367
381
  })();
368
382
  context = (function() {
369
383
  var _i, _len, _ref, _results;
370
-
371
384
  _ref = pipes.shift().split('<');
372
385
  _results = [];
373
386
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -390,11 +403,10 @@
390
403
  if (dependencies = context.shift()) {
391
404
  options.dependencies = dependencies.split(/\s+/);
392
405
  }
393
- return _this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
406
+ return _this.bindings.push(new Rivets[binding](_this, node, type, key, keypath, options));
394
407
  };
395
408
  parse = function(node) {
396
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;
397
-
398
410
  if (__indexOf.call(skipNodes, node) < 0) {
399
411
  if (node.nodeType === Node.TEXT_NODE) {
400
412
  parser = Rivets.TextTemplateParser;
@@ -403,18 +415,17 @@
403
415
  if (!(tokens.length === 1 && tokens[0].type === parser.types.text)) {
404
416
  startToken = tokens[0], restTokens = 2 <= tokens.length ? __slice.call(tokens, 1) : [];
405
417
  node.data = startToken.value;
406
- switch (startToken.type) {
407
- case 0:
408
- node.data = startToken.value;
409
- break;
410
- case 1:
411
- buildBinding(node, 'textNode', startToken.value);
418
+ if (startToken.type === 0) {
419
+ node.data = startToken.value;
420
+ } else {
421
+ buildBinding('TextBinding', node, null, startToken.value);
412
422
  }
413
423
  for (_i = 0, _len = restTokens.length; _i < _len; _i++) {
414
424
  token = restTokens[_i];
415
- node.parentNode.appendChild((text = document.createTextNode(token.value)));
425
+ text = document.createTextNode(token.value);
426
+ node.parentNode.appendChild(text);
416
427
  if (token.type === 1) {
417
- buildBinding(text, 'textNode', token.value);
428
+ buildBinding('TextBinding', text, null, token.value);
418
429
  }
419
430
  }
420
431
  }
@@ -457,7 +468,7 @@
457
468
  attribute = _ref3[_l];
458
469
  if (bindingRegExp.test(attribute.name)) {
459
470
  type = attribute.name.replace(bindingRegExp, '');
460
- buildBinding(node, type, attribute.value);
471
+ buildBinding('Binding', node, type, attribute.value);
461
472
  }
462
473
  }
463
474
  }
@@ -479,7 +490,6 @@
479
490
 
480
491
  View.prototype.select = function(fn) {
481
492
  var binding, _i, _len, _ref, _results;
482
-
483
493
  _ref = this.bindings;
484
494
  _results = [];
485
495
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -493,7 +503,6 @@
493
503
 
494
504
  View.prototype.bind = function() {
495
505
  var binding, _i, _len, _ref, _results;
496
-
497
506
  _ref = this.bindings;
498
507
  _results = [];
499
508
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -505,7 +514,6 @@
505
514
 
506
515
  View.prototype.unbind = function() {
507
516
  var binding, _i, _len, _ref, _results;
508
-
509
517
  _ref = this.bindings;
510
518
  _results = [];
511
519
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -517,7 +525,6 @@
517
525
 
518
526
  View.prototype.sync = function() {
519
527
  var binding, _i, _len, _ref, _results;
520
-
521
528
  _ref = this.bindings;
522
529
  _results = [];
523
530
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@@ -529,7 +536,6 @@
529
536
 
530
537
  View.prototype.publish = function() {
531
538
  var binding, _i, _len, _ref, _results;
532
-
533
539
  _ref = this.select(function(b) {
534
540
  return b.binder.publishes;
535
541
  });
@@ -543,7 +549,6 @@
543
549
 
544
550
  View.prototype.update = function(models) {
545
551
  var binding, key, model, _i, _len, _ref, _results;
546
-
547
552
  if (models == null) {
548
553
  models = {};
549
554
  }
@@ -574,7 +579,6 @@
574
579
 
575
580
  TextTemplateParser.parse = function(template, delimiters) {
576
581
  var index, lastIndex, lastToken, length, substring, tokens, value;
577
-
578
582
  tokens = [];
579
583
  length = template.length;
580
584
  index = 0;
@@ -657,7 +661,6 @@
657
661
  },
658
662
  getInputValue: function(el) {
659
663
  var o, _i, _len, _results;
660
-
661
664
  if (window.jQuery != null) {
662
665
  el = jQuery(el);
663
666
  switch (el[0].type) {
@@ -704,7 +707,6 @@
704
707
  },
705
708
  routine: function(el, value) {
706
709
  var _ref;
707
-
708
710
  if (el.type === 'radio') {
709
711
  return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
710
712
  } else {
@@ -722,7 +724,6 @@
722
724
  },
723
725
  routine: function(el, value) {
724
726
  var _ref;
725
-
726
727
  if (el.type === 'radio') {
727
728
  return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
728
729
  } else {
@@ -749,7 +750,6 @@
749
750
  },
750
751
  routine: function(el, value) {
751
752
  var o, _i, _len, _ref, _ref1, _ref2, _results;
752
-
753
753
  if (window.jQuery != null) {
754
754
  el = jQuery(el);
755
755
  if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
@@ -782,7 +782,6 @@
782
782
  block: true,
783
783
  bind: function(el) {
784
784
  var attr, declaration;
785
-
786
785
  if (this.marker == null) {
787
786
  attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
788
787
  declaration = el.getAttribute(attr);
@@ -794,12 +793,10 @@
794
793
  },
795
794
  unbind: function() {
796
795
  var _ref;
797
-
798
796
  return (_ref = this.nested) != null ? _ref.unbind() : void 0;
799
797
  },
800
798
  routine: function(el, value) {
801
799
  var key, model, models, options, _ref;
802
-
803
800
  if (!!value === (this.nested == null)) {
804
801
  if (value) {
805
802
  models = {};
@@ -823,7 +820,8 @@
823
820
  }
824
821
  },
825
822
  update: function(models) {
826
- return this.nested.update(models);
823
+ var _ref;
824
+ return (_ref = this.nested) != null ? _ref.update(models) : void 0;
827
825
  }
828
826
  },
829
827
  unless: {
@@ -859,7 +857,6 @@
859
857
  block: true,
860
858
  bind: function(el) {
861
859
  var attr;
862
-
863
860
  if (this.marker == null) {
864
861
  attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
865
862
  this.marker = document.createComment(" rivets: " + this.type + " ");
@@ -871,7 +868,6 @@
871
868
  },
872
869
  unbind: function(el) {
873
870
  var view, _i, _len, _ref, _results;
874
-
875
871
  if (this.iterated != null) {
876
872
  _ref = this.iterated;
877
873
  _results = [];
@@ -883,8 +879,7 @@
883
879
  }
884
880
  },
885
881
  routine: function(el, collection) {
886
- var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
887
-
882
+ var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results;
888
883
  modelName = this.args[0];
889
884
  collection = collection || [];
890
885
  if (this.iterated.length > collection.length) {
@@ -905,7 +900,7 @@
905
900
  _ref1 = this.view.models;
906
901
  for (key in _ref1) {
907
902
  model = _ref1[key];
908
- if ((_ref2 = data[key]) == null) {
903
+ if (data[key] == null) {
909
904
  data[key] = model;
910
905
  }
911
906
  }
@@ -915,9 +910,9 @@
915
910
  formatters: this.view.options.formatters,
916
911
  config: {}
917
912
  };
918
- _ref3 = this.view.options.config;
919
- for (k in _ref3) {
920
- v = _ref3[k];
913
+ _ref2 = this.view.options.config;
914
+ for (k in _ref2) {
915
+ v = _ref2[k];
921
916
  options.config[k] = v;
922
917
  }
923
918
  options.config.preloadData = true;
@@ -936,7 +931,6 @@
936
931
  },
937
932
  update: function(models) {
938
933
  var data, key, model, view, _i, _len, _ref, _results;
939
-
940
934
  data = {};
941
935
  for (key in models) {
942
936
  model = models[key];
@@ -955,7 +949,6 @@
955
949
  },
956
950
  "class-*": function(el, value) {
957
951
  var elClass;
958
-
959
952
  elClass = " " + el.className + " ";
960
953
  if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) {
961
954
  return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim();
@@ -970,12 +963,6 @@
970
963
  }
971
964
  };
972
965
 
973
- Rivets.internalBinders = {
974
- textNode: function(node, value) {
975
- return node.data = value != null ? value : '';
976
- }
977
- };
978
-
979
966
  Rivets.components = {};
980
967
 
981
968
  Rivets.config = {
@@ -995,7 +982,6 @@
995
982
  exports.config = Rivets.config;
996
983
  exports.configure = function(options) {
997
984
  var property, value;
998
-
999
985
  if (options == null) {
1000
986
  options = {};
1001
987
  }
@@ -1006,7 +992,6 @@
1006
992
  };
1007
993
  return exports.bind = function(el, models, options) {
1008
994
  var view;
1009
-
1010
995
  if (models == null) {
1011
996
  models = {};
1012
997
  }