3

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()
    }
}
JotaBe
  • 38,030
  • 8
  • 98
  • 117
Sixtease
  • 790
  • 8
  • 18

2 Answers2

10

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.

RP Niemeyer
  • 114,592
  • 18
  • 291
  • 211
  • 1
    I had to add the element as first agument but than it worked. Thanks! I wonder if this is documented somewhere. I was *so* missing this method. :-) – Sixtease Mar 07 '13 at 14:34
0

Have you tried using jQuery in your custom binder:

ko.bindingHandlers.foo = {
    init: function(el,val,bindings,model,context) {
        $(el).attr('data-bind', 'click: clickHandler');
    }
}
Paul Manzotti
  • 5,107
  • 21
  • 27