Skip to content
Browse files

Add checked binding for checkboxes.

  • Loading branch information...
1 parent f2acc0d commit a8f72d160a0d8aa0be4b3414f1de5163dd43ab5f @andrewdavey committed
Showing with 37 additions and 0 deletions.
  1. +1 −0 build/output/knockout-min.js
  2. +18 −0 build/output/knockout.js
  3. +18 −0 src/binding/defaultBindings.js
View
1 build/output/knockout-min.js
@@ -33,6 +33,7 @@ a[d][c.options_value]:a[d];g.value=i.toString();g.innerHTML=(typeof c.options_te
ko.bindingHandlers.selectedOptions={getSelectedValuesFromSelectNode:function(e){var a=[];e=e.childNodes;for(var c=0,b=e.length;c<b;c++){var d=e[c];d.tagName=="OPTION"&&d.selected&&a.push(d.value)}return a},init:function(e,a,c){if(ko.isWriteableObservable(a))ko.utils.registerEventHandler(e,"change",function(){a(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this))});else c._ko_property_writers&&c._ko_property_writers.value&&ko.utils.registerEventHandler(e,"change",function(){c._ko_property_writers.value(ko.bindingHandlers.selectedOptions.getSelectedValuesFromSelectNode(this))})},
update:function(e,a){if(e.tagName!="SELECT")throw Error("values binding applies only to SELECT elements");var c=ko.utils.unwrapObservable(a);if(c&&typeof c.length=="number")for(var b=e.childNodes,d=0,f=b.length;d<f;d++){var g=b[d];if(g.tagName=="OPTION")g.selected=ko.utils.arrayIndexOf(c,g.value)>=0}}};ko.bindingHandlers.text={update:function(e,a){a=ko.utils.unwrapObservable(a);typeof e.innerText=="string"?e.innerText=a:e.textContent=a}};
ko.bindingHandlers.css={update:function(e,a){a=a||{};for(var c in a)if(typeof c=="string"){var b=ko.utils.unwrapObservable(a[c]);ko.utils.toggleDomNodeCssClass(e,c,b)}}};
+ko.bindingHandlers.checked={init:function(e,a,c){var b=c.valueUpdate||"click";if(ko.isWriteableObservable(a))ko.utils.registerEventHandler(e,b,function(){a(!!this.checked)});else c._ko_property_writers&&c._ko_property_writers.value&&ko.utils.registerEventHandler(e,b,function(){c._ko_property_writers.value(!!this.checked)})},update:function(e,a){e.checked=ko.utils.unwrapObservable(a)}};
ko.templateEngine=function(){this.renderTemplate=function(){throw"Override renderTemplate in your ko.templateEngine subclass";};this.isTemplateRewritten=function(){throw"Override isTemplateRewritten in your ko.templateEngine subclass";};this.rewriteTemplate=function(){throw"Override rewriteTemplate in your ko.templateEngine subclass";};this.createJavaScriptEvaluatorBlock=function(){throw"Override createJavaScriptEvaluatorBlock in your ko.templateEngine subclass";}};
ko.templateRewriting=function(){var e=/(<[a-z]+(\s+(?!data-bind=)[a-z0-9]+(=(\"[^\"]*\"|\'[^\']*\'))?)*\s+)data-bind=(["'])(.*?)\5/g;return{ensureTemplateIsRewritten:function(a,c){c.isTemplateRewritten(a)||c.rewriteTemplate(a,function(b){return ko.templateRewriting.memoizeBindingAttributeSyntax(b,c)})},memoizeBindingAttributeSyntax:function(a,c){return a.replace(e,function(b,d,f,g,i,h,j){b=j;b=ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(b);return c.createJavaScriptEvaluatorBlock("ko.templateRewriting.applyMemoizedBindingsToNextSibling(function() { return (function() { return { "+
b+" } })() })")+d})},applyMemoizedBindingsToNextSibling:function(a){return ko.memoization.memoize(function(c){c.nextSibling&&ko.applyBindingsToNode(c.nextSibling,a,null)})}}}();
View
18 build/output/knockout.js
@@ -780,6 +780,24 @@ ko.bindingHandlers.css = {
}
}
}
+};
+
+// Binds a boolean observable to the checked state of an <input type="checkbox"/> element.
+ko.bindingHandlers.checked = {
+ init: function (element, value, allBindings) {
+ var eventName = allBindings.valueUpdate || "click";
+ if (ko.isWriteableObservable(value))
+ ko.utils.registerEventHandler(element, eventName, function () {
+ value(!!this.checked);
+ });
+ else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
+ ko.utils.registerEventHandler(element, eventName, function () {
+ allBindings._ko_property_writers.value(!!this.checked);
+ });
+ },
+ update: function (element, value) {
+ element.checked = ko.utils.unwrapObservable(value);
+ }
};/// <reference path="../utils.js" />
ko.templateEngine = function () {
View
18 src/binding/defaultBindings.js
@@ -164,4 +164,22 @@ ko.bindingHandlers.css = {
}
}
}
+};
+
+// Binds a boolean observable to the checked state of an <input type="checkbox"/> element.
+ko.bindingHandlers.checked = {
+ init: function (element, value, allBindings) {
+ var eventName = allBindings.valueUpdate || "click";
+ if (ko.isWriteableObservable(value))
+ ko.utils.registerEventHandler(element, eventName, function () {
+ value(!!this.checked);
+ });
+ else if (allBindings._ko_property_writers && allBindings._ko_property_writers.value)
+ ko.utils.registerEventHandler(element, eventName, function () {
+ allBindings._ko_property_writers.value(!!this.checked);
+ });
+ },
+ update: function (element, value) {
+ element.checked = ko.utils.unwrapObservable(value);
+ }
};

0 comments on commit a8f72d1

Please sign in to comment.
Something went wrong with that request. Please try again.