Prevent "with" binding to delete DOM elements (Knockout.js)

Knockouters

I came to rely on binding with binding to establish contextual nesting. while I like how KO manipulates the DOM based on the state of related elements in some cases, sometimes I just want to bind the consequences without removing the DOM.

Does anyone know if it is possible to prevent the prevention of DOM manipulation at the anchor level of individual elements?

Thanks Vinney

+4
source share
1 answer

Version 2.2+ from knockout will not clear the DOM element when with bound initially to an object (or other plausible value). Alternatively, you can use the withlight binding that I collected some time ago. It will be attached only to the object (and not to the observable).

 ko.bindingHandlers['withlight'] = { 'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var bindingValue = valueAccessor(); if (typeof bindingValue != 'object' || bindingValue === null) throw new Error('withlight must be used with an object'); var innerContext = bindingContext['createChildContext'](bindingValue); ko.applyBindingsToDescendants(innerContext, element); return { 'controlsDescendantBindings': true }; } }; 
+5
source

All Articles