Implement own knockout bindings

There are a lot of custom bindings in the internet to use with KnockoutJS. Sometimes they do not work because the init() method is called before the associated HTML code has not been added to the DOM at this moment. This is because the Visto JavaScript initializes everything hidden to avoid GUI flickering. An example with this problem is a custom binding which creates an JQuery accordion in the init() method.

The original code might look like this:

ko.bindingHandlers.myCustomBinding = {
    init: function (element, valueAccessor) {
        /* your code here */
    }
};

To run the code when the HTML is visible it is possible to register a callback method which is called when the view has been added to the DOM:

ko.bindingHandlers.myCustomBinding = {
    init: function (element, valueAccessor) {
        /* called before visible in DOM  */
        visto.addInitializer(function () {
            /* called when visible in DOM  */
        });
    }
};

Important: Use the addInitializer method only in KnockoutJS binding initializer methods (init() method)!


Last edited Jul 26, 2013 at 9:54 PM by rsuter, version 6

Comments

No comments yet.