-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Work around IE < 8 issue - it insists that OPTION nodes must have val…
…ue attributes (even if they don't), so use marker text to identify when an option is associated with a DOM data value
- Loading branch information
1 parent
320e191
commit 1b39996
Showing
2 changed files
with
43 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
(function () { | ||
// Normally, SELECT elements and their OPTIONs can only take value of type 'string' (because the values | ||
// are stored on DOM attributes). ko.selectExtensions provides a way for SELECTs/OPTIONs to have values | ||
// that are arbitrary objects. This is very convenient when implementing things like cascading dropdowns. | ||
ko.selectExtensions = { | ||
readValue : function(element) { | ||
if (element.tagName == 'OPTION') { | ||
var valueAttributeValue = element.getAttribute("value"); | ||
if (valueAttributeValue !== null) | ||
return valueAttributeValue; | ||
return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey); | ||
} else if (element.tagName == 'SELECT') | ||
return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined; | ||
else | ||
return element.value; | ||
}, | ||
|
||
writeValue: function(element, value) { | ||
if (element.tagName == 'SELECT') { | ||
for (var i = element.options.length - 1; i >= 0; i--) { | ||
if (ko.selectExtensions.readValue(element.options[i]) == value) { | ||
element.selectedIndex = i; | ||
break; | ||
} | ||
} | ||
} else | ||
element.value = value; | ||
} | ||
}; | ||
// Normally, SELECT elements and their OPTIONs can only take value of type 'string' (because the values | ||
// are stored on DOM attributes). ko.selectExtensions provides a way for SELECTs/OPTIONs to have values | ||
// that are arbitrary objects. This is very convenient when implementing things like cascading dropdowns. | ||
ko.selectExtensions = { | ||
readValue : function(element) { | ||
if (element.tagName == 'OPTION') { | ||
var valueAttributeValue = element.getAttribute("value"); | ||
if (valueAttributeValue !== ko.bindingHandlers.options.optionValueDomDataKey) | ||
return valueAttributeValue; | ||
return ko.utils.domData.get(element, ko.bindingHandlers.options.optionValueDomDataKey); | ||
} else if (element.tagName == 'SELECT') | ||
return element.selectedIndex >= 0 ? ko.selectExtensions.readValue(element.options[element.selectedIndex]) : undefined; | ||
else | ||
return element.value; | ||
}, | ||
|
||
writeValue: function(element, value) { | ||
if (element.tagName == 'OPTION') { | ||
ko.utils.domData.set(element, ko.bindingHandlers.options.optionValueDomDataKey, value); | ||
element.value = ko.bindingHandlers.options.optionValueDomDataKey; | ||
} else if (element.tagName == 'SELECT') { | ||
for (var i = element.options.length - 1; i >= 0; i--) { | ||
if (ko.selectExtensions.readValue(element.options[i]) == value) { | ||
element.selectedIndex = i; | ||
break; | ||
} | ||
} | ||
} else | ||
element.value = value; | ||
} | ||
}; | ||
})(); |