Added new options parameter 'optionsDisabled' #398

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+33 −2
Diff settings

Always

Just for now

@@ -453,6 +453,30 @@ describe('Binding: Options', {
ko.applyBindings({ myValues: modelValues }, testNode);
var displayedText = ko.utils.arrayMap(testNode.childNodes[0].childNodes, function (node) { return node.innerText || node.textContent; });
value_of(displayedText).should_be(["bob (manager)", "frank (coder & tester)"]);
+ },
+
+ 'Should accept optionsDisabled param to set the disabled subproperty of the model values': function () {
+ var modelValues = new ko.observableArray([
+ { name: 'bob', id: ko.observable(6), disabled: ko.observable(true) },
+ { name: ko.observable('frank'), id: 13, disabled: true },
+ { name: ko.observable('jill'), id: 24, disabled: false }
+ ]);
+ testNode.innerHTML = "<select data-bind='options:myValues, optionsText: \"name\", optionsValue: \"id\", optionsDisabled: \"disabled\"'><option>should be deleted</option></select>";
+ ko.applyBindings({ myValues: modelValues }, testNode);
+ var disabledValues = ko.utils.arrayMap(testNode.childNodes[0].childNodes,
+ function (node) {
+ var _disabled = node.getAttribute('disabled');
+ if (_disabled === 'disabled')
+ return node.value;
+ });
+ var enabledValues = ko.utils.arrayMap(testNode.childNodes[0].childNodes,
+ function (node) {
+ var _disabled = node.getAttribute('disabled');
+ if (_disabled === undefined || _disabled === null)
+ return node.value;
+ });
+ value_of(disabledValues).should_be([6, 13, undefined]);
+ value_of(enabledValues).should_be([undefined, undefined, 24]);
},
'Should update the SELECT node\'s options if the model changes': function () {
@@ -230,8 +230,15 @@ ko.bindingHandlers['options'] = {
// Apply a value to the option element
var optionValue = typeof allBindings['optionsValue'] == "string" ? value[i][allBindings['optionsValue']] : value[i];
optionValue = ko.utils.unwrapObservable(optionValue);
- ko.selectExtensions.writeValue(option, optionValue);
-
+ ko.selectExtensions.writeValue(option, optionValue);
+
+ if (allBindings['optionsDisabled']) {
+ var disabledValue = typeof allBindings['optionsDisabled'] == "string" ? value[i][allBindings['optionsDisabled']] : allBindings['optionsDisabled'];
+ disabledValue = ko.utils.unwrapObservable(disabledValue);
+ if (disabledValue)
+ option.setAttribute('disabled', 'disabled');
+ }
+
// Apply some text to the option element
var optionsTextValue = allBindings['optionsText'];
var optionText;