From 412fbf8976f93b8622314c7351c072c8ca0dd681 Mon Sep 17 00:00:00 2001 From: Michael Best Date: Fri, 15 Mar 2013 16:02:42 -1000 Subject: [PATCH] Use strict equality when determining whether to update the value binding. --- spec/defaultBindings/valueBehaviors.js | 9 +++++++++ src/binding/defaultBindings/value.js | 7 +------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/spec/defaultBindings/valueBehaviors.js b/spec/defaultBindings/valueBehaviors.js index 0206faf66..2dc9805cc 100644 --- a/spec/defaultBindings/valueBehaviors.js +++ b/spec/defaultBindings/valueBehaviors.js @@ -34,6 +34,15 @@ describe('Binding: Value', function() { expect(testNode.childNodes[0].value).toEqual("456"); }); + it('For observable values, should update on change if new value is \'strictly\' different from previous value', function() { + var myobservable = new ko.observable("+123"); + testNode.innerHTML = ""; + ko.applyBindings({ someProp: myobservable }, testNode); + expect(testNode.childNodes[0].value).toEqual("+123"); + myobservable(123); + expect(testNode.childNodes[0].value).toEqual("123"); + }); + it('For writeable observable values, should catch the node\'s onchange and write values back to the observable', function () { var myobservable = new ko.observable(123); testNode.innerHTML = ""; diff --git a/src/binding/defaultBindings/value.js b/src/binding/defaultBindings/value.js index a4febb7bc..b7e2ce687 100755 --- a/src/binding/defaultBindings/value.js +++ b/src/binding/defaultBindings/value.js @@ -47,12 +47,7 @@ ko.bindingHandlers['value'] = { var valueIsSelectOption = ko.utils.tagNameLower(element) === "select"; var newValue = ko.utils.unwrapObservable(valueAccessor()); var elementValue = ko.selectExtensions.readValue(element); - var valueHasChanged = (newValue != elementValue); - - // JavaScript's 0 == "" behavious is unfortunate here as it prevents writing 0 to an empty text box (loose equality suggests the values are the same). - // We don't want to do a strict equality comparison as that is more confusing for developers in certain cases, so we specifically special case 0 != "" here. - if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) - valueHasChanged = true; + var valueHasChanged = (newValue !== elementValue); if (valueHasChanged) { var applyValueAction = function () { ko.selectExtensions.writeValue(element, newValue); };