908701de by Michael Richards

Merge pull request #34 from mikeric/formatter-arguments

Formatter arguments
2 parents 6205d410 7d8f7d5c
1 (function() { 1 (function() {
2 var Rivets, attributeBinding, bidirectionals, bindEvent, eventBinding, getInputValue, rivets, unbindEvent, 2 var Rivets, attributeBinding, bidirectionals, bindEvent, eventBinding, getInputValue, rivets, unbindEvent,
3 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 3 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
4 __slice = [].slice,
4 __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; }; 5 __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; };
5 6
6 Rivets = {}; 7 Rivets = {};
...@@ -28,6 +29,8 @@ ...@@ -28,6 +29,8 @@
28 29
29 this.set = __bind(this.set, this); 30 this.set = __bind(this.set, this);
30 31
32 this.formattedValue = __bind(this.formattedValue, this);
33
31 if (this.bindType === "event") { 34 if (this.bindType === "event") {
32 this.routine = eventBinding(this.type); 35 this.routine = eventBinding(this.type);
33 } else { 36 } else {
...@@ -35,13 +38,20 @@ ...@@ -35,13 +38,20 @@
35 } 38 }
36 } 39 }
37 40
38 Binding.prototype.set = function(value) { 41 Binding.prototype.formattedValue = function(value) {
39 var formatter, _i, _len, _ref; 42 var args, formatter, id, _i, _len, _ref, _ref1;
40 _ref = this.formatters; 43 _ref = this.formatters;
41 for (_i = 0, _len = _ref.length; _i < _len; _i++) { 44 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
42 formatter = _ref[_i]; 45 formatter = _ref[_i];
43 value = Rivets.config.formatters[formatter](value); 46 args = formatter.split(/\s+/);
47 id = args.shift();
48 value = (_ref1 = Rivets.config.formatters)[id].apply(_ref1, [value].concat(__slice.call(args)));
44 } 49 }
50 return value;
51 };
52
53 Binding.prototype.set = function(value) {
54 value = this.formattedValue(value);
45 if (this.bindType === "event") { 55 if (this.bindType === "event") {
46 this.routine(this.el, value, this.currentListener); 56 this.routine(this.el, value, this.currentListener);
47 return this.currentListener = value; 57 return this.currentListener = value;
......
1 (function(){var a,b,c,d,e,f,g,h,i=function(a,b){return function(){return a.apply(b,arguments)}},j=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};a={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function c(c,d,f,g,h,j){this.el=c,this.type=d,this.bindType=f,this.model=g,this.keypath=h,this.formatters=j!=null?j:[],this.unbind=i(this.unbind,this),this.publish=i(this.publish,this),this.bind=i(this.bind,this),this.set=i(this.set,this),this.bindType==="event"?this.routine=e(this.type):this.routine=a.routines[this.type]||b(this.type)}return c.prototype.set=function(b){var c,d,e,f;f=this.formatters;for(d=0,e=f.length;d<e;d++)c=f[d],b=a.config.formatters[c](b);return this.bindType==="event"?(this.routine(this.el,b,this.currentListener),this.currentListener=b):this.routine(this.el,b)},c.prototype.bind=function(){a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath));if(this.bindType==="bidirectional")return d(this.el,"change",this.publish)},c.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,f(c))},c.prototype.unbind=function(){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if(this.bindType==="bidirectional")return this.el.removeEventListener("change",this.publish)},c}(),a.View=function(){function b(a,b){this.el=a,this.models=b,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.el.jquery&&(this.el=this.el.get(0)),this.build()}return b.prototype.bindingRegExp=function(){var b;return b=a.config.prefix,b?new RegExp("^data-"+b+"-"):/^data-/},b.prototype.build=function(){var b,d,e,f,g,h,i,k,l=this;this.bindings=[],b=this.bindingRegExp(),d=/^on-/,f=function(e){var f,g,h,i,k,m,n,o,p,q,r,s;r=e.attributes,s=[];for(p=0,q=r.length;p<q;p++)f=r[p],b.test(f.name)?(g="attribute",o=f.name.replace(b,""),n=function(){var a,b,c,d;c=f.value.split("|"),d=[];for(a=0,b=c.length;a<b;a++)m=c[a],d.push(m.trim());return d}(),k=n.shift().split("."),i=l.models[k.shift()],h=k.join("."),d.test(o)?(o=o.replace(d,""),g="event"):j.call(c,o)>=0&&(g="bidirectional"),s.push(l.bindings.push(new a.Binding(e,o,g,i,h,n)))):s.push(void 0);return s},f(this.el),i=this.el.getElementsByTagName("*"),k=[];for(g=0,h=i.length;g<h;g++)e=i[g],k.push(f(e));return k},b.prototype.bind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.unbind());return e},b}(),d=function(a,b,c){return window.addEventListener?a.addEventListener(b,c):a.attachEvent(b,c)},h=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c):a.detachEvent(b,c)},f=function(a){switch(a.type){case"text":case"textarea":case"password":case"select-one":case"radio":return a.value;case"checkbox":return a.checked}},e=function(a){return function(b,c,e){c&&d(b,a,c);if(e)return h(b,a,e)}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},c=["value","checked","unchecked"],a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.type==="radio"?a.checked=a.value===b:a.checked=!!b},unchecked:function(a,b){return a.type==="radio"?a.checked=a.value!==b:a.checked=!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=b||""},value:function(a,b){return a.value=b},text:function(a,b){return a.innerText!=null?a.innerText=b||"":a.textContent=b||""}},a.config={preloadData:!0},g={routines:a.routines,config:a.config,configure:function(b){var c,d,e;b==null&&(b={}),e=[];for(c in b)d=b[c],e.push(a.config[c]=d);return e},bind:function(b,c){var d;return c==null&&(c={}),d=new a.View(b,c),d.bind(),d}},typeof module!="undefined"&&module!==null?module.exports=g:this.rivets=g}).call(this);
...\ No newline at end of file ...\ No newline at end of file
1 (function(){var a,b,c,d,e,f,g,h,i=function(a,b){return function(){return a.apply(b,arguments)}},j=[].slice,k=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};a={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function c(c,d,f,g,h,j){this.el=c,this.type=d,this.bindType=f,this.model=g,this.keypath=h,this.formatters=j!=null?j:[],this.unbind=i(this.unbind,this),this.publish=i(this.publish,this),this.bind=i(this.bind,this),this.set=i(this.set,this),this.formattedValue=i(this.formattedValue,this),this.bindType==="event"?this.routine=e(this.type):this.routine=a.routines[this.type]||b(this.type)}return c.prototype.formattedValue=function(b){var c,d,e,f,g,h,i;h=this.formatters;for(f=0,g=h.length;f<g;f++)d=h[f],c=d.split(/\s+/),e=c.shift(),b=(i=a.config.formatters)[e].apply(i,[b].concat(j.call(c)));return b},c.prototype.set=function(a){return a=this.formattedValue(a),this.bindType==="event"?(this.routine(this.el,a,this.currentListener),this.currentListener=a):this.routine(this.el,a)},c.prototype.bind=function(){a.config.adapter.subscribe(this.model,this.keypath,this.set),a.config.preloadData&&this.set(a.config.adapter.read(this.model,this.keypath));if(this.bindType==="bidirectional")return d(this.el,"change",this.publish)},c.prototype.publish=function(b){var c;return c=b.target||b.srcElement,a.config.adapter.publish(this.model,this.keypath,f(c))},c.prototype.unbind=function(){a.config.adapter.unsubscribe(this.model,this.keypath,this.set);if(this.bindType==="bidirectional")return this.el.removeEventListener("change",this.publish)},c}(),a.View=function(){function b(a,b){this.el=a,this.models=b,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.el.jquery&&(this.el=this.el.get(0)),this.build()}return b.prototype.bindingRegExp=function(){var b;return b=a.config.prefix,b?new RegExp("^data-"+b+"-"):/^data-/},b.prototype.build=function(){var b,d,e,f,g,h,i,j,l=this;this.bindings=[],b=this.bindingRegExp(),d=/^on-/,f=function(e){var f,g,h,i,j,m,n,o,p,q,r,s;r=e.attributes,s=[];for(p=0,q=r.length;p<q;p++)f=r[p],b.test(f.name)?(g="attribute",o=f.name.replace(b,""),n=function(){var a,b,c,d;c=f.value.split("|"),d=[];for(a=0,b=c.length;a<b;a++)m=c[a],d.push(m.trim());return d}(),j=n.shift().split("."),i=l.models[j.shift()],h=j.join("."),d.test(o)?(o=o.replace(d,""),g="event"):k.call(c,o)>=0&&(g="bidirectional"),s.push(l.bindings.push(new a.Binding(e,o,g,i,h,n)))):s.push(void 0);return s},f(this.el),i=this.el.getElementsByTagName("*"),j=[];for(g=0,h=i.length;g<h;g++)e=i[g],j.push(f(e));return j},b.prototype.bind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.unbind());return e},b}(),d=function(a,b,c){return window.addEventListener?a.addEventListener(b,c):a.attachEvent(b,c)},h=function(a,b,c){return window.removeEventListener?a.removeEventListener(b,c):a.detachEvent(b,c)},f=function(a){switch(a.type){case"text":case"textarea":case"password":case"select-one":case"radio":return a.value;case"checkbox":return a.checked}},e=function(a){return function(b,c,e){c&&d(b,a,c);if(e)return h(b,a,e)}},b=function(a){return function(b,c){return c?b.setAttribute(a,c):b.removeAttribute(a)}},c=["value","checked","unchecked"],a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.type==="radio"?a.checked=a.value===b:a.checked=!!b},unchecked:function(a,b){return a.type==="radio"?a.checked=a.value!==b:a.checked=!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=b||""},value:function(a,b){return a.value=b},text:function(a,b){return a.innerText!=null?a.innerText=b||"":a.textContent=b||""}},a.config={preloadData:!0},g={routines:a.routines,config:a.config,configure:function(b){var c,d,e;b==null&&(b={}),e=[];for(c in b)d=b[c],e.push(a.config[c]=d);return e},bind:function(b,c){var d;return c==null&&(c={}),d=new a.View(b,c),d.bind(),d}},typeof module!="undefined"&&module!==null?module.exports=g:this.rivets=g}).call(this);
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -63,4 +63,29 @@ describe('Rivets.Binding', function() { ...@@ -63,4 +63,29 @@ describe('Rivets.Binding', function() {
63 }); 63 });
64 }); 64 });
65 }); 65 });
66
67 describe('formattedValue()', function() {
68 it('applies the current formatters on the supplied value', function() {
69 rivets.config.formatters = {
70 awesome: function(value) { return 'awesome ' + value }
71 };
72 binding.formatters.push('awesome');
73 expect(binding.formattedValue('hat')).toBe('awesome hat');
74 });
75
76 describe('with a multi-argument formatter string', function() {
77 beforeEach(function() {
78 rivets.config.formatters = {
79 awesome: function(value, prefix) {
80 return prefix + ' awesome ' + value;
81 }
82 };
83 binding.formatters.push('awesome super');
84 });
85
86 it('applies the formatter with arguments', function() {
87 expect(binding.formattedValue('jacket')).toBe('super awesome jacket');
88 });
89 });
90 });
66 }); 91 });
......
...@@ -134,36 +134,4 @@ describe('Routines', function() { ...@@ -134,36 +134,4 @@ describe('Routines', function() {
134 }); 134 });
135 }); 135 });
136 }); 136 });
137
138 describe('selected', function() {
139 describe('with a truthy value', function() {
140 it('selects the element', function() {
141 rivets.routines.selected(el, true);
142 expect(el.selected).toBe(true);
143 });
144 });
145
146 describe('with a falsey value', function() {
147 it('unselects the element', function() {
148 rivets.routines.selected(el, false);
149 expect(el.selected).toBe(false);
150 });
151 });
152 });
153
154 describe('unselected', function() {
155 describe('with a truthy value', function() {
156 it('unselects the element', function() {
157 rivets.routines.unselected(el, true);
158 expect(el.selected).toBe(false);
159 });
160 });
161
162 describe('with a falsey value', function() {
163 it('selects the element', function() {
164 rivets.routines.unselected(el, false);
165 expect(el.selected).toBe(true);
166 });
167 });
168 });
169 }); 137 });
......
...@@ -20,11 +20,20 @@ class Rivets.Binding ...@@ -20,11 +20,20 @@ class Rivets.Binding
20 else 20 else
21 @routine = Rivets.routines[@type] || attributeBinding @type 21 @routine = Rivets.routines[@type] || attributeBinding @type
22 22
23 # Applies all the current formatters to the supplied value and returns the
24 # formatted value.
25 formattedValue: (value) =>
26 for formatter in @formatters
27 args = formatter.split /\s+/
28 id = args.shift()
29 value = Rivets.config.formatters[id] value, args...
30
31 value
32
23 # Sets the value for the binding. This Basically just runs the binding routine 33 # Sets the value for the binding. This Basically just runs the binding routine
24 # with the suplied value and applies any formatters. 34 # with the suplied value formatted.
25 set: (value) => 35 set: (value) =>
26 for formatter in @formatters 36 value = @formattedValue value
27 value = Rivets.config.formatters[formatter] value
28 37
29 if @bindType is "event" 38 if @bindType is "event"
30 @routine @el, value, @currentListener 39 @routine @el, value, @currentListener
......