I'm having a problem with a custom knockout binding handler that creates a context. The demonstration is here: http://jsfiddle.net/gf3tfarz/14/
When 'container' is applied to an observable array, it's not updating the children elements.
<div data-bind="container: { data: selectedCountry().ids }">
<p>Error: <span data-bind="text: $container.data().length"></span></p>
<div data-bind="foreach: $container.data">
<p>Error: <span data-bind="text: $data"></span></p>
</div>
</div>
This is the custom binding handler:
ko.bindingHandlers.container = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var innerBindingContext = bindingContext.extend({
$container: ko.utils.unwrapObservable(valueAccessor())
});
ko.applyBindingsToDescendants(innerBindingContext, element);
return { controlsDescendantBindings: true };
}
};
I'd like a way that both examples using 'container' present in the demonstration work.
Note that using "with" it works.
You shouldnt be extending the bindingContext directly, you should create a child binding context. Then it will work how you expect it to.
See the bottom of this page for more info... http://knockoutjs.com/documentation/custom-bindings-controlling-descendant-bindings.html