In all of my knockout custom binding handlers, I always omit the update callback. My reasoning is that I can do all of the work I need to do inside of the init callback. In there, I can create computed's, call applyBindingsToNode (if I need) or setup manual subscriptions to subscribables and do my DOM manipulation inside those subscriptions.
My concern is that the default bindings in knockout are built using the update callback only.(Update: Not anymore, value and some other bindings only use init now) Is there any downside to doing all the work inside the init callback? Is there anything I'm missing? Thanks!
Creating computeds and subscriptions in the init
function, as you are doing, is completely supported in Knockout since version 3.0. It is a more advanced technique than using update
since it means you need to take more care to ensure you're capturing and responding to all dependencies, as well as disposing subscriptions at the right time.
For reference, the update
function itself is handled quite simply in Knockout (modified slightly for clarity):
var handlerUpdateFn = handler.update;
if (typeof handlerUpdateFn == "function") {
ko.computed(
function () {
handlerUpdateFn(node, getValueAccessor(bindingKey), allBindings,
bindingContext.$data, bindingContext);
},
null,
{ disposeWhenNodeIsRemoved: node }
);
}