Skip to content

Commit

Permalink
Fix for knockout#562, value binding should always write when triggered.
Browse files Browse the repository at this point in the history
  • Loading branch information
rniemeyer committed Sep 14, 2012
1 parent cb594d0 commit 4fe4f0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions spec/defaultBindings/valueBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ describe('Binding: Value', {
value_of(myobservable()).should_be("some user-entered value");
},

'For writeable observable values, should always write when triggered, even when value is the same': function () {
var validValue = ko.observable(123);
var isValid = ko.observable(true);
var valueForEditing = ko.computed({
read: validValue,
write: function(newValue) {
if (!isNaN(newValue)) {
isValid(true);
validValue(newValue);
} else {
isValid(false);
}
}
});

testNode.innerHTML = "<input data-bind='value: valueForEditing' />";
ko.applyBindings({ valueForEditing: valueForEditing}, testNode);

//set initial valid value
testNode.childNodes[0].value = "1234";
ko.utils.triggerEvent(testNode.childNodes[0], "change");
value_of(validValue()).should_be("1234");
value_of(valid()).should_be(true);

//set to an invalid value
testNode.childNodes[0].value = "1234a";
ko.utils.triggerEvent(testNode.childNodes[0], "change");
value_of(validValue()).should_be("1234");
value_of(valid()).should_be(false);

//set to a valid value where the current value of the writeable computed is the same as the written value
testNode.childNodes[0].value = "1234";
ko.utils.triggerEvent(testNode.childNodes[0], "change");
value_of(validValue()).should_be("1234");
value_of(valid()).should_be(true);
},

'For non-observable property values, should catch the node\'s onchange and write values back to the property': function () {
var model = { modelProperty123: 456 };
testNode.innerHTML = "<input data-bind='value: modelProperty123' />";
Expand Down
2 changes: 1 addition & 1 deletion src/binding/defaultBindings/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ko.bindingHandlers['value'] = {
var valueUpdateHandler = function() {
var modelValue = valueAccessor();
var elementValue = ko.selectExtensions.readValue(element);
ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue, /* checkIfDifferent: */ true);
ko.expressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue);
}

// Workaround for https://github.com/SteveSanderson/knockout/issues/122
Expand Down

0 comments on commit 4fe4f0f

Please sign in to comment.