Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout: add another binding in a custom binding init

I want to create a custom binding as a shorthand for adding other bindings -- like a macro.

<div data-bind="foo: 1"></div>

should do the same thing as

<div data-bind="click: clickHandler, css: { someClass: someObservable }, ...">
</div>

Something like:

ko.bindingHandlers.foo = {
    init: function(el,val,bindings,model,context) {
        // some way to add { click: clickHandler } to bindings()
    }
}
like image 595
Sixtease Avatar asked May 05 '26 06:05

Sixtease


1 Answers

You can call ko.applyBindingsToNode from within the init of your binding handler like:

ko.applyBindingsToNode({ click: someHandler, text: someText });

If you are applying something like a control-flow, then you would want to pass the context in the second argument.

like image 98
RP Niemeyer Avatar answered May 07 '26 20:05

RP Niemeyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!