0

Hi i have an observable array

this.item = ko.observableArray({
     tax: ko.observable(8.0);
     price: ko.observable(100.00);
     eachTotal:  ko.computed(function() {
           this.tax() * this.price();
     });
});

I am getting error saying object [Window] does not have tax method.

How would I do this?

Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53

2 Answers2

1

well what i had to do was this.

ko.utils.arrayForEach(myModel.item(), function(it) {
     it.eachTotal = ko.computed(function() {
        return it.tax() * it.price();
     });
});

I am wondering if this is the most efficient way to do this?

Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
1

this is possibly not as you expected in javascript. It is the context of the caller.
As a remedy one often sets a self or _self to this in the beginning of the constructor.
Look through the examples again at knockout and pay attention to self.

Or if arrayForEach works like underscore there is a third parameter which is the "this context".

LosManos
  • 7,195
  • 6
  • 56
  • 107