0

If i have a custom-binding that aggregates others like the following:

ko.bindingHandlers.binding1 = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var ctx = ko.utils.unwrapObservable(valueAccessor());

        ko.applyBindingsToNode(element, { 'binding2': ctx });        
        ko.applyBindingsToNode(element, { 'binding3': ctx });        
    }
};

"allBindingsAccessor" is returning only the current binding on its init function.

I was expecting that it returns binding1 and binding2 on binding2 "init" function and binding1, 2, and 3 on binding3 "init" function.

The following fiddle show the behaviour i mentioned. http://jsfiddle.net/J3sjq/5/

The problem is that my binding3 depends upon other bindings presence, like binding2 for instance.

What's the best way to implement that behaviour? I was thinking about modifying the context to inform of other bindings presence, but it sounded a little bit like a hack.

Thanks in advance.

EDIT:

I have updated my fiddle to show more of the original problem, i tried to simplify but i may have overdone it.

http://jsfiddle.net/J3sjq/6/

In the new fiddle, the binding1 is replaced by an initFieldStatus that initializes 3 other complex bindings each one calculating an input field status, the isInputAtWarning binding depends upon other bindings declared on the element.

Hopefully this will clarify the question.

Thankyou.

EDIT 2:

I provided a fiddle with a more real world example of what i am trying to implement.

http://jsfiddle.net/J3sjq/8/

  • Please show more code. So far, it seems as though you have a situation where your bindings, whether dependent or not, need to be declared in your initial ko.applyBindings and this attempt at declaring them through a bindingHandler are just making your brain hurt. – beauXjames Dec 02 '13 at 19:45
  • Thanks, just updated the question to add more details. – Ricardo Medeiros Penna Dec 02 '13 at 20:13
  • You may be misunderstanding the purpose of a bindingHandler. It's not something you apply to nodes in the way you are trying to. You can, though, stack bindings to your element, just like you do with initFieldStatus and value. Your goal here is fuzzy. Maybe you would do your readers a favor by explaining what you're trying to do and not so much about what you're doing. Providing the jsFiddle is enough for that. – beauXjames Dec 02 '13 at 20:20
  • `allBindingsAccessor` only gives access to bindings applied at the same time. When you call `ko.applyBindingsToNode`, those bindings are distinct from any other binding operation. – Michael Best Dec 02 '13 at 20:55
  • @MichaelBest Do you know a way i can get the bindings already applied to node? Is that possible? – Ricardo Medeiros Penna Dec 03 '13 at 10:57
  • @beauXjames I'll modify the question a bit more with a new fiddle, but solving the problem is less important to me than understanding the matter in question, that is, how can i get the bindings already applied to a node so that i can have bindings dependent on the presence of another binding. – Ricardo Medeiros Penna Dec 03 '13 at 11:11
  • @RicardoMedeirosPenna, I assume that when you say 'node' you mean element in your DOM. In that case, if you can collect your node via jquery or js or whatever then try ko.dataFor or ko.contextFor(element)...you'll know that an element is tied into knockout if it's got a __ko__XXXXXXXXXX property added to it. At that point, you can collect the bindings and will then know that the element has, in fact, been initialized via knockout. – beauXjames Dec 04 '13 at 01:08

1 Answers1

0

Rather than calling ko.applyBindingsToNode, you can directly call the init function of a binding.

ko.bindingHandlers.isInputAtWarning.init(
    element, function() { return observable.isInputAtWarning }, 
    allBindingsAccessor, viewModel, bindingContext);

http://jsfiddle.net/mbest/J3sjq/9/

But if you're only calling this directly, you might as well just make it a function.

function isInputAtWarning(element, value, allBindingsAccessor) {
    ...
}

...    

isInputAtWarning(element, observable.isInputAtWarning, allBindingsAccessor);
Michael Best
  • 16,623
  • 1
  • 37
  • 70
  • Thanks, that's what i was looking for. Does not accumulate with the other added/initiated bindings, but i can adapt that. For future readers and to make merit to @beauXjames, his questions made me think and give up the ideia of the dependent binding for the "real world example", i ended up just passing the same observable to the initInputStatus, isInputAtWarning etc that is passed to the value binding, this way they can check for validation rules, i left the dependent bindings for more extreme cases. – Ricardo Medeiros Penna Dec 04 '13 at 14:01