Question

Under the Explicitly Subscribing to Observables section of the Knockout documentation, there is a reference to an event parameter of the subscribe function, but the only two examples given on that page are change and beforeChange.

By way of example, I tried passing in "focus" as the third parameter but that didn't work. I'm not too surprised as "focus" is an event of a DOM Element rather than a knockout observable, but nonetheless it could theoretically have setup a subscription to the focus event for all elements bound to that observable.

Is there a list of all events that can be subscribed to manually using Knockout's observable.subscribe function?

Was it helpful?

Solution

It make sens to use "event" binding in your case.

Because there are only two ways to notify subscribers of observable variable: beforeChange and change.

In knockoutJs code there is simple chain of if blocks which check if event is specified, and if event is equal to beforeChange. That's basically all logic which goes there, so no other events fired.


Part form knockoutJS which implements this logic:

  self["notifySubscribers"] = function(value, event) {
    if (!event || event === defaultEvent) {
      self._rateLimitedChange(value);
    } else if (event === beforeChange) {
      self._rateLimitedBeforeChange(value);
    } else {
      self._origNotifySubscribers(value, event);
    }
  };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top