Let's say I have a viewModel like this:
function ViewModel() {
var $t = this;
this.a = ko.observable(5);
this.f = function(val) {
return $t.a() * (val === 'simple' ? 2: 10);
};
this.c = ko.computed(function() {
return $t.a() * 20;
});
};
ko.applyBindings(new ViewModel());
and HTML like this:
<div><input data-bind="value: a"/></div>
<div data-bind="text: f('simple')"></div>
<div data-bind="text: f('complex')"></div>
<div data-bind="text: c"></div>
this results in this initial page (the [5] depicts an input field):
[5]
10
50
100
When I change the 5 in the input field into 10 I see this:
[10]
20
100
200
So the bindings work. See a working demo here: http://jsfiddle.net/1vrz71fs/2/.
What I want to understand is the difference between using ko.computed(...)
and a plain old function, they both seem to work just fine. Are there are cons / pros of using the one over the other. The function approach seems to be much more convenient since it accepts parameters which the ko.computed
doesn't let me do...