Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout.js subscribe is not getting called on the first set

I have a simple model class with observables. I simply want to subscribe to its sets. Here is the code that I have:

var dto = function (data) {
    var self = this;
    self.Value1 = ko.observable(data.Value1);
    self.Value1.subscribe(function(){
         console.log('here');
    });   
};

the console.log doesn't get called when the Value1 is first set (i.e. ko.observable(data.Value1)

How do I set it up that subsribe function happens on both initial and when it changes.

like image 449
jmogera Avatar asked Jan 02 '14 06:01

jmogera


2 Answers

There is no real support for triggering the subscribe function for the initial values.

What you can do is to call the valueHasMutated function after your subscribe:

self.Value1.subscribe(function(){
     console.log('here');
}); 
self.Value1.valueHasMutated();

Or you can just set your initial values after you've subscribed:

var dto = function (data) {
    var self = this;
    self.Value1 = ko.observable(); // only declare but not assign
    self.Value1.subscribe(function(){
         console.log('here');
    });   
    self.Value1(data.Value1); // assign initial value
};
like image 157
nemesv Avatar answered Oct 18 '22 09:10

nemesv


To improve a bit on richard's answer. If this is a recurring situation, where you don't know if an observable has gotten a value already but you want to call the subscriber if it has, then you can extend the KO subscribable with this function that has the same signature as the original subscribe:

ko.subscribable.fn.subscribeAndCall = function(subscribingFunction, context, event) {
  var subscribableValue = this();

  this.subscribe(subscribingFunction, context, event);

  if (subscribableValue !== undefined) {
    subscribingFunction.call(context, subscribableValue);
  }
};

It also works for initial values that are falsy and will not accidentally trigger any other subscribers of the observable.

Example usage for the original question:

self.Value1.subscribeAndCall(function(){
     console.log('here');
}, this);
like image 42
bjornalm Avatar answered Oct 18 '22 09:10

bjornalm