user1470994
user1470994

Reputation: 311

Knockout/JS Referencing Observable In Other Function

Just getting back in to the swing of things and seem to be losing my mind...

I have a function (model):

function Users(data) {
   var self = this;
   self.UserComments = ko.observableArray();
   //...some other thingies
}

And then...

function viewModel() {
    var self = this;
    //How do I access self.UserComments from the Users function?
}

Bonus points, question is somewhere in the comments. :)

Cheers

Edit:

To make it clearer I actually need the self.UserComments array (with data intact) accessible in my viewModel.

The Users function will add a comment to the self.UserComments array each time data is added in the viewModel,

Upvotes: 0

Views: 51

Answers (2)

Eric Hartford
Eric Hartford

Reputation: 18032

You would need to put one in your viewModel.

function viewModel() {
    var self = this;
    self.users = new Users({bla: 1});
    // Now I can reference it.
    self.count = ko.computed(function(){
        return self.users.UserComments().length;
    });
}

Upvotes: 1

cyber_rookie
cyber_rookie

Reputation: 665

This should work:

function viewModel() {
    var self = this;
    self.UserComments = ko.observableArray();
}

Upvotes: 0

Related Questions