javascripttemplatesknockout.jsbinding-context

Knockout Template: Own BindingContext, same Parents


I'm writing a bindingHandler and want to create a binding-context which has the same parents/parent-contexts like the current bindingContext. Read: no extra binding-context-hierarchy.

Background: I write a dialog-binding-handler
A dom-node has a dialog-binding like data-bind="dialog: { childTemplate: 'childTemplateId' }"
After an click, a generic dialog-template gets rendered and attached to body. You also specify a child-template (= is the desired concrete view), which gets choosen by the generic dialog-template by the binding-context-variable $childTemplateName.
The child-template should have no clue, that there is an extra layer (= generic dialog template) between it and the level containing the dialog-binding-handler. So $parent, $parent.$parent, $parent.$parent.$parent,... are the same in the child-template and the dom-node having data-bind="dialog: ...".

I want to have an extra binding-context so I can extend it with properties specific for this "dialog-call", so the current binding-context doesn't get polluted.


Solution

  • OK, it seems that
    bindingContext.$parentContext.createChildContext(childViewModel);
    works perfecly!
    So using $parentContext is the (acutally logical) way to go.

    (if I remember correctly I had tried this previously with no success with pervious versions of knockout...but nevermind)