Skip to content
Michael Best edited this page Jul 24, 2013 · 4 revisions

Here's a custom binding that allows you run a function after an element is updated. It takes advantage of the fact that the update functions for all bindings are called whenever any of the dependencies are updated.

ko.bindingHandlers.runafter = {
    update: function(element, valueAccessor, allBindingsAccessor) {
        // add dependency on all other bindings
        ko.toJS(allBindingsAccessor());
        setTimeout(function() {
            var value = valueAccessor();
            if (typeof value != 'function' || ko.isObservable(value))
                throw new Error('run must be used with a function');
            value(element);
        });
    }
};

Here's a fiddle showing it working.