user3378165
user3378165

Reputation: 6916

observable and computed are not working properly Knockout

I have the following knockout code that implements the folowing role:

field1 + field2 -field3 = field4

$(function () {
ko.applyBindings(new AppViewModel());
});

function AppViewModel() {
this.original = ko.observable(0);
this.improvements = ko.observable(0);
this.depreciation = ko.observable(0);

this.total= ko.computed(function () {
    var total= 0;
    total= this.original() + this.improvements() - this.depreciation();
    return total;
}, this);
}

But for some reason it's not working properly, this.original is always multiplied by 10.

for example:

1 + 1 - 1 = 10

Any idea what can causes this?

This is my HTML:

<div class="calc-form">
     <label>Original Purchase Price</label>
     <input type="text" id="original" data-bind="value: original" />

     <label>+ Improvements</label>
     <input type="text" id="improvements" data-bind="value: improvements" />

     <label>- Depreciation</label>
     <input type="text" id="depreciation" data-bind="value: depreciation" />

     <input type="button" class="calcbutton" value="Calculate" />
     <input type="button" class="calcbuttonreset" value="reset" />
     <p>= Total</p>
     <span data-bind="text: total"></span>
</div>

Upvotes: 4

Views: 150

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075447

Remember that the value of input elements is always a string. "1" + "1" - "1" is "11" - "1" is 10 because when either operand is a string, + is string concatenation, not addition; but - is always subtraction, so it coerces its operands to numbers.

You need to parse them, via +x or parseFloat(x) or Number(x) or (if they're meant to be whole numbers) parseInt(x, 10), etc.:

total = parseFloat(this.original())
        + parseFloat(this.improvements())
        - parseFloat(this.depreciation());

Example:

$(function() {
  ko.applyBindings(new AppViewModel());
});

function AppViewModel() {
  this.original = ko.observable(0);
  this.improvements = ko.observable(0);
  this.depreciation = ko.observable(0);

  this.total = ko.computed(function() {
    var total = 0;
    total = parseFloat(this.original())
            + parseFloat(this.improvements())
            - parseFloat(this.depreciation());
    return total;
  }, this);
}
<div class="calc-form">
  <label>Original Purchase Price</label>
  <input type="text" id="original" data-bind="value: original" />

  <label>+ Improvements</label>
  <input type="text" id="improvements" data-bind="value: improvements" />

  <label>- Depreciation</label>
  <input type="text" id="depreciation" data-bind="value: depreciation" />

  <input type="button" class="calcbutton" value="Calculate" />
  <input type="button" class="calcbuttonreset" value="reset" />
  <p>= Total</p>
  <span data-bind="text: total"></span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

If you're going to be working with numeric inputs a lot, you might want to give yourself a specific binding for them:

// "numValue" binding handler (just an example)
ko.bindingHandlers.numValue = {
  init: function(element, valueAccessor) {
    function numValueHandler() {
      valueAccessor()(parseFloat(this.value));
    }
    $(element).on("input change", numValueHandler)
              .val(ko.unwrap(valueAccessor()));
    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
      $(element).off("input change", numValueHandler);
    });
  },
  update: function(element, valueAccessor) {
    element.value = ko.unwrap(valueAccessor());
  }
};

Then:

<input type="text" id="original" data-bind="numValue: original" />
<!-- ---------------------------------------^^^^^^^^           -->

// "numValue" binding handler (just an example)
ko.bindingHandlers.numValue = {
  init: function(element, valueAccessor) {
    function numValueHandler() {
      valueAccessor()(parseFloat(this.value));
    }
    $(element).on("input change", numValueHandler)
              .val(ko.unwrap(valueAccessor()));
    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
      $(element).off("input change", numValueHandler);
    });
  },
  update: function(element, valueAccessor) {
    element.value = ko.unwrap(valueAccessor());
  }
};

$(function() {
  ko.applyBindings(new AppViewModel());
});

function AppViewModel() {
  this.original = ko.observable(0);
  this.improvements = ko.observable(0);
  this.depreciation = ko.observable(0);

  this.total = ko.computed(function() {
    var total = 0;
    total = this.original() + this.improvements() - this.depreciation();
    return total;
  }, this);
}
<div class="calc-form">
  <label>Original Purchase Price</label>
  <input type="text" id="original" data-bind="numValue: original" />

  <label>+ Improvements</label>
  <input type="text" id="improvements" data-bind="numValue: improvements" />

  <label>- Depreciation</label>
  <input type="text" id="depreciation" data-bind="numValue: depreciation" />

  <input type="button" class="calcbutton" value="Calculate" />
  <input type="button" class="calcbuttonreset" value="reset" />
  <p>= Total</p>
  <span data-bind="text: total"></span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

Upvotes: 3

Related Questions