0

Is there a way to listen for applyBindings event in ko?

I have searched google and SO and can't find a way. The answer needs to be supported ie not calling internal functions that may get changed in the future by the KO developer(s).

The following question is not similar as it asks how to check if applyBindings has been called. I want to know when it is called.

Community
  • 1
  • 1
Robert Ye
  • 48
  • 4

1 Answers1

1

In JavaScript, it's pretty easy to intercept any method call because you can just modify the object. Thus you can replace ko.applyBindings with your own function that does whatever you want (before or after you call the original):

ko.applyBindings = (function(original) {
    return function () {
        original.apply(this, arguments);
        alert('ko.applyBindings called');
    };
})(ko.applyBindings);

http://jsfiddle.net/mbest/4a2vK/

NOTE: This method will behave differently between the debug and release versions of Knockout. Knockout uses ko.applyBindings internally for the template binding, and when using the release version, it will always call the internal ko.applyBindings function, whereas in the debug version, it will call our replacement function.

Michael Best
  • 16,623
  • 1
  • 37
  • 70