Possible duplicate:
knockoutjs: can we create a dependObservable function with a parameter?
In my view model, I have a function that returns the sum of elements depending on the type of element:
var ViewModel = function(data) {
var self = this;
this.Results = ko.observableArray(data);
this.totalPerType = function(type) {
var total = 0;
for (var index in self.Results()) {
if (self.Results()[index].Type == type)
total += self.Results()[index].Quantity;
}
return total;
};
};
When the user edits one of the elements, the total value is not updated automatically, since it is not calculated. Is it possible to change the totalPerType function to a computed observable without having to enter a type parameter in the view model (saving it as a parameter)?
I created a script to make it easier to try something: http://jsfiddle.net/7PK9r/
source
share