Ruslan
Ruslan

Reputation: 10147

Subscribe doesn't work with Knockout Model as function

I think I'm struggling with the basics here. The code:

var VehicleSearchViewModel = function() {

  this.VehicleVariantId = ko.observable(0);

  this.VehicleVariantId.subscribe(function (id) {
      console.log(id);
  });
};

from the function outside of this, I do the following:

VehicleSearchViewModel.VehicleVariantId = 777;

...and console.log doesn't fire. Albeit if I type VehicleSearchViewModel.VehicleVariantId in the console I can see it's been updated with the new value. What's the right way of doing this?

Upvotes: 0

Views: 447

Answers (1)

Kyeotic
Kyeotic

Reputation: 19847

Knockout observables are functions. You need to set them by passing the new value as a parameter.

VehicleSearchViewModel.VehicleVariantId(777);

You really should go through the tutorials, you are going to have a lot of questions that can be answered by them.

Upvotes: 2

Related Questions