Austin Matthew
Austin Matthew

Reputation: 43

Knockout computed observable not working

I am using a Knockout computed observable to multiply two other observables. Unfortunately, the computed observable does not seem to be outputting any value.

var boyle1 = {
volume1_text: ko.observable(parseInt(2)),
volume1_select: ko.observable(parseInt(2))
};
boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);
ko.applyBindings(boyle1);

Both other observables work perfectly, and are easily binded to elements on the page. What am I doing wrong?

Upvotes: 0

Views: 3467

Answers (1)

Matthew Cox
Matthew Cox

Reputation: 13672

It has something to do with your html binding to the computed. As you indicated it was in fact, a typo in the markup.

Markup

<input data-bind="value: volume1_text" />
<input data-bind="value: volume1_select" />
<br /><br />    
<span data-bind="text: volume"></span>

Model

var boyle1 = {
   volume1_text: ko.observable(parseInt(2)),
   volume1_select: ko.observable(parseInt(2))
};

boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);

ko.applyBindings(boyle1);

Here is a working example of your model and problem

http://jsfiddle.net/YBr2m/1

Upvotes: 1

Related Questions