From 4fe4f0fc1313e82cf84e031e2e8412631d51a256 Mon Sep 17 00:00:00 2001 From: Ryan Niemeyer Date: Thu, 13 Sep 2012 22:26:18 -0500 Subject: [PATCH] Fix for #562, value binding should always write when triggered. --- spec/defaultBindings/valueBehaviors.js | 37 ++++++++++++++++++++++++++ src/binding/defaultBindings/value.js | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/spec/defaultBindings/valueBehaviors.js b/spec/defaultBindings/valueBehaviors.js index 7b269b042..7044c518b 100644 --- a/spec/defaultBindings/valueBehaviors.js +++ b/spec/defaultBindings/valueBehaviors.js @@ -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 = ""; + 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 = ""; diff --git a/src/binding/defaultBindings/value.js b/src/binding/defaultBindings/value.js index c8bb0ee80..2140c374b 100755 --- a/src/binding/defaultBindings/value.js +++ b/src/binding/defaultBindings/value.js @@ -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