Skip to content

Commit

Permalink
Merge pull request knockout#462 from SteveSanderson/462-value-binding…
Browse files Browse the repository at this point in the history
…-uses-strict-equality

bindingHandler.value's valueHasChanged field is 'false' but maybe should be 'true'
  • Loading branch information
rniemeyer committed Apr 23, 2013
2 parents 6f4fa61 + 412fbf8 commit 56c0436
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 9 additions & 0 deletions spec/defaultBindings/valueBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<input data-bind='value:someProp' />";
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 = "<input data-bind='value:someProp' />";
Expand Down
7 changes: 1 addition & 6 deletions src/binding/defaultBindings/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); };
Expand Down

0 comments on commit 56c0436

Please sign in to comment.