3

Possible Duplicate:
knockoutjs: can we create a dependentObservable function with a parameter?

In my viewmodel, I have a function that returns the sum of items based on the type of item:

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 items, the total isn't updated automatically, because it isn't a computed observable. Is it possible to change the function totalPerType into a computed observable, without having to put the type parameter into the viewmodel (keeping it as a parameter)?

I created a Fiddle to make it easier to try some things: http://jsfiddle.net/7PK9r/

Community
  • 1
  • 1
Wesley Cabus
  • 121
  • 1
  • 1
  • 7

1 Answers1

2

I think this answers your question:

knockoutjs: can we create a dependentObservable function with a parameter?

(Note: dependentObservable is what computed observables used to be called prior to v2.0 of knockout.js)

Community
  • 1
  • 1
Mark Robinson
  • 13,128
  • 13
  • 63
  • 81
  • 1
    I had to change the example a bit to make it work, but thanks! You can find the working example here: [http://jsfiddle.net/2pB9Y/41/](http://jsfiddle.net/2pB9Y/41/) – Wesley Cabus May 16 '12 at 05:34