Elliot
Elliot

Reputation: 13835

Access observables in other javascript outside of model with knockout?

So I have a view model like so:

var viewModel = function() {
    var self = this;
    this.chartSeries = ko.observableArray(['All Series']);
}

ko.applyBindings(new viewModel());

$(function (){
//I want to access it here
}

How do I accomplish this?

when I try viewModel().chartSeries, viewModel.chartSeries , viewModel.chartSeries() etc. I get undefined errors.

Upvotes: 0

Views: 2752

Answers (1)

John Earles
John Earles

Reputation: 7194

You are getting undefined errors because you don't have an instance of the view model in scope. The easiest thing would be to do:

var vm = new viewModel();
ko.applyBindings(vm);

$(function (){
  // do stuff with vm
  vm.chartSeries.push('Series A');
}

Upvotes: 5

Related Questions