Skip to content

Commit

Permalink
QOL update for selectExtensions.js; cleanup nested switches
Browse files Browse the repository at this point in the history
Nested switch statements are radically poor form for readability and structural programming standards. Cleaned up the writeValue function to only use a single switch, for greatly enhanced readability.
  • Loading branch information
DodekaHydra committed Aug 19, 2013
1 parent 77de67e commit 09638bd
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/binding/selectExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@
writeValue: function(element, value) {
switch (ko.utils.tagNameLower(element)) {
case 'option':
switch(typeof value) {
case "string":
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, undefined);
if (hasDomDataExpandoProperty in element) { // IE <= 8 throws errors if you delete non-existent properties from a DOM node
delete element[hasDomDataExpandoProperty];
}
element.value = value;
break;
default:
// Store arbitrary object using DomData
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value);
element[hasDomDataExpandoProperty] = true;
if (typeof value === "string") {
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, undefined);
if (hasDomDataExpandoProperty in element) { // IE <= 8 throws errors if you delete non-existent properties from a DOM node
delete element[hasDomDataExpandoProperty];
}
element.value = value;
}
else {
// Store arbitrary object using DomData
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value);
element[hasDomDataExpandoProperty] = true;

// Special treatment of numbers is just for backward compatibility. KO 1.2.1 wrote numerical values to element.value.
element.value = typeof value === "number" ? value : "";
break;
// Special treatment of numbers is just for backward compatibility. KO 1.2.1 wrote numerical values to element.value.
element.value = typeof value === "number" ? value : "";
}
break;
case 'select':
Expand Down

0 comments on commit 09638bd

Please sign in to comment.