I am constructing a tree structure using jQuery mobile collapsible-set
and binding data at different level of the tree on collapse expand event.
Data binding is working perfectly fine, but when user re-opens the tree, again binding happens and multiple binding error
comes.
How to avoid this? I tried cleanNode
and the !!ko.dataFor
. Nothing is working..
Here is my code below. I am using stop binding because only on click of some node binding should be applied to only to that section.
HTML
<div id="tree" data-role="collapsible-set" data-collapsed-icon="carat-d"
data-expanded-icon="carat-u" data-iconpos="right"
data-bind="foreach:FolderObject">
<div data-role="collapsible" class="Folder" data-inset="false"
data-bind="attr:{'id':$index()}">
<span style="margin-left:10px;" data-bind="text:DisplayName"></span>
<!--Subtree-->
<div data-bind="attr:{'id':'Jobs'+$index()} ">
<!-- ko stopBinding: true -->
<div data-bind="foreach:SubTreeObject" data-role="collapsible-set">
<div data-role="collapsible" class="JobFolder" data-inset="false"
data-bind="attr:{'id':$index()}">
<span style="margin-left:10px;" data-bind="text:DisplayName">
</span>
</div>
</div>
<--/ko-->
</div>
</div>
</div>
JS
$('#tree').on('collapsibleexpand', '.Folder', function (evt) {
var id=this.id;
var x = $('#Jobs' + id + ' ' + 'div[data-role="collapsible-set"]');
ko.applyBindings(VM2, x[0]); // VM2 has 'SubTreeObject' data
});