0
votes

This might be pretty basic but I just can not seem to figure it out. The problem goes

I have a view model with an observable array 'self.product' and want to access it from a nested model as shown below. when I run this I get an error that product does not exist. I have not included the code but there is an ajax call that gets data and pushes it to 'product'. Also using a click binding on the function 'test' does print the value.

I though it might be a scoping issue with the 'self' but since I use the 'product' array in a foreach binding in the markup I can not just change self.product... to var product. how might I accomplish this?

var viewModel = function () {
        var self = this;
        self.product = ko.observableArray([]);
var orderItem = function (data) {
            var self = this;
            self.PlayDuration = ko.computed(function () {
                var foo  = self.product()[0].something();
                return foo;
            });

        }
self.updateTotals = function () {
            console.log(self.product()[0].something());
        }
});
1
You are hiding self with the inner declaration on orderItem (which I don't see even being used). If you put your code in a fiddle, we can help more. - Kyeotic
post code that generates the error. - Brandon
orderItem is populated from AJAX as well. I will try put together something for fiddler however it is very dependent on data from server. the code fails at this line var foo = self.product()[0].something(); - Mark Hollas
one call gets data from the server and pushes it to 'self.product' then when it is done another call gets data and pushes orderItems into another observable array. it fails the first time it hits the computed function - Mark Hollas
@Tyrsius if you can put your comment in the form of an answer I will accept it. it was all about the scoping - Mark Hollas

1 Answers

1
votes
var root = this;

Add that line to the outermost viewmodel and use it in the inner viewmodels.