javascript - How to partially (re)apply bindings to descendants with knockout? -


i created custom knockout binding wraps given div in expander. custom binding moves given content div contant-container div of expander. after moving content, knockout bindings of content child nodes not work more (e.g. click binding button inside content div). therefore have reapply knockout bindings.

this works in cases. however, if content div contains example knockout foreach binding, reapplying bindings means content duplicated.

example usage of expander binding:

<div         data-bind="expander: { title: 'datacollectionforms'}"> <div>   <div class="expander-content">     <button            data-bind="click: $root.addaction, text: 'hinzufügen'"></button>     <div       data-bind="foreach: listofbuttons">       <button                data-bind="click: $root.buttonclickaction">        </button>     </div>   </div> </div> </div> 

my code moving content div:

function moveexpandercontenttocontentcontainer($contentcontainer, expandercontent) {   try {      //move expander content content container     //the content not cloned moved. more info see:     //https://api.jquery.com/append/     var $expandercontent = $(expandercontent);     $contentcontainer.append($expandercontent);     $contentcontainer.addclass('expander-content-container panel-body');     $expandercontent.addclass('expander-content');      ko.applybindingstodescendants(bindingcontext, expandercontent);     } catch (appendexception) {     var errormessage = 'could not append expander-content expander-content-container.';     logger.logerror(errormessage, appendexception, self, true);   } 

}

  • if remove line

ko.applybindingstodescendants(bindingcontext, expandercontent);

the click actions of 3 buttons not work more: enter image description here

  • if keep line, click actions work buttons duplicated: enter image description here

=> how can update bindings of moved content in way fixes click bindings , not duplicate buttons?

=> if moving work flow not work @ all, better way create custom knockout binding wraps given content in collapsable expander?

i find related articles no solution specific issue:

i solved issue not moving content div @ building expander around it.

my original strategy have reusable expander view + view model , move content div original location newly composed expander view. moving around bound content no idea, guess.

the new strategy adapts existing divs , composes header expander.

nevertheless thank thoughts.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -