1aacccf8 by Michael Richards

Merge pull request #33 from mikeric/radio-group-fix

Radio group fix
2 parents 5d8926e0 247a6f47
...@@ -104,7 +104,6 @@ With that routine defined, the following binding will update the element's color ...@@ -104,7 +104,6 @@ With that routine defined, the following binding will update the element's color
104 - data-disabled 104 - data-disabled
105 - data-checked 105 - data-checked
106 - data-unchecked 106 - data-unchecked
107 - data-selected
108 - data-*[attribute]* 107 - data-*[attribute]*
109 - data-on-*[event]* 108 - data-on-*[event]*
110 109
......
...@@ -205,9 +205,9 @@ ...@@ -205,9 +205,9 @@
205 case 'textarea': 205 case 'textarea':
206 case 'password': 206 case 'password':
207 case 'select-one': 207 case 'select-one':
208 case 'radio':
208 return el.value; 209 return el.value;
209 case 'checkbox': 210 case 'checkbox':
210 case 'radio':
211 return el.checked; 211 return el.checked;
212 } 212 }
213 }; 213 };
...@@ -233,7 +233,7 @@ ...@@ -233,7 +233,7 @@
233 }; 233 };
234 }; 234 };
235 235
236 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']; 236 bidirectionals = ['value', 'checked', 'unchecked'];
237 237
238 Rivets.routines = { 238 Rivets.routines = {
239 enabled: function(el, value) { 239 enabled: function(el, value) {
...@@ -243,16 +243,18 @@ ...@@ -243,16 +243,18 @@
243 return el.disabled = !!value; 243 return el.disabled = !!value;
244 }, 244 },
245 checked: function(el, value) { 245 checked: function(el, value) {
246 if (el.type === 'radio') {
247 return el.checked = el.value === value;
248 } else {
246 return el.checked = !!value; 249 return el.checked = !!value;
250 }
247 }, 251 },
248 unchecked: function(el, value) { 252 unchecked: function(el, value) {
253 if (el.type === 'radio') {
254 return el.checked = el.value !== value;
255 } else {
249 return el.checked = !value; 256 return el.checked = !value;
250 }, 257 }
251 selected: function(el, value) {
252 return el.selected = !!value;
253 },
254 unselected: function(el, value) {
255 return el.selected = !value;
256 }, 258 },
257 show: function(el, value) { 259 show: function(el, value) {
258 return el.style.display = value ? '' : 'none'; 260 return el.style.display = value ? '' : 'none';
......
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":return a.value;case"checkbox":case"radio":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","selected","unselected"],a.routines={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:function(a,b){return a.checked=!!b},unchecked:function(a,b){return a.checked=!b},selected:function(a,b){return a.selected=!!b},unselected:function(a,b){return a.selected=!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=[].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
......
...@@ -124,8 +124,8 @@ unbindEvent = (el, event, fn) -> ...@@ -124,8 +124,8 @@ unbindEvent = (el, event, fn) ->
124 # Returns the current input value for the specified element. 124 # Returns the current input value for the specified element.
125 getInputValue = (el) -> 125 getInputValue = (el) ->
126 switch el.type 126 switch el.type
127 when 'text', 'textarea', 'password', 'select-one' then el.value 127 when 'text', 'textarea', 'password', 'select-one', 'radio' then el.value
128 when 'checkbox', 'radio' then el.checked 128 when 'checkbox' then el.checked
129 129
130 # Returns an element binding routine for the specified attribute. 130 # Returns an element binding routine for the specified attribute.
131 eventBinding = (event) -> (el, bind, unbind) -> 131 eventBinding = (event) -> (el, bind, unbind) ->
...@@ -139,7 +139,7 @@ attributeBinding = (attr) -> (el, value) -> ...@@ -139,7 +139,7 @@ attributeBinding = (attr) -> (el, value) ->
139 139
140 # Bindings that should also be observed for changes on the DOM element in order 140 # Bindings that should also be observed for changes on the DOM element in order
141 # to propagate those changes back to the model object. 141 # to propagate those changes back to the model object.
142 bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'] 142 bidirectionals = ['value', 'checked', 'unchecked']
143 143
144 # Core binding routines. 144 # Core binding routines.
145 Rivets.routines = 145 Rivets.routines =
...@@ -148,13 +148,15 @@ Rivets.routines = ...@@ -148,13 +148,15 @@ Rivets.routines =
148 disabled: (el, value) -> 148 disabled: (el, value) ->
149 el.disabled = !!value 149 el.disabled = !!value
150 checked: (el, value) -> 150 checked: (el, value) ->
151 if el.type is 'radio'
152 el.checked = el.value is value
153 else
151 el.checked = !!value 154 el.checked = !!value
152 unchecked: (el, value) -> 155 unchecked: (el, value) ->
156 if el.type is 'radio'
157 el.checked = el.value isnt value
158 else
153 el.checked = !value 159 el.checked = !value
154 selected: (el, value) ->
155 el.selected = !!value
156 unselected: (el, value) ->
157 el.selected = !value
158 show: (el, value) -> 160 show: (el, value) ->
159 el.style.display = if value then '' else 'none' 161 el.style.display = if value then '' else 'none'
160 hide: (el, value) -> 162 hide: (el, value) ->
......