8

The Knockout.js bootstrap module in Magento 2 includes the following module via a define dependency.

 Magento_Ui/js/lib/knockout/extender/bound-nodes

Technically speaking, this module creates wrappers for the ko.applyBindings and ko.cleanNode methods. These wrappers will stash certain view models and nodes in a private javascript WeakMap, and then return a single registry object that will let you access items in the WeakMap.

However, it's not clear what end-user-programmer facing features this enables. Does anyone here know what the Magento_Ui/js/lib/knockout/extender/bound-nodes module is actually for? Or is familiar enough with Knockout.js internals to figure things out?

1 Answer 1

2

The Magento_Ui/js/lib/knockout/extender/bound-nodes registry will, theoretically, let you fetch a list of HTML-DOM nodes that are scopeded to a particular view model. i.e. you should be able to say something like this

boundNodes = requirejs('Magento_Ui/js/lib/knockout/extender/bound-nodes');
reg = requirejs('uiRegistry');    
viewModel = reg.get('customer');
console.log( boundNodes.get(viewModel) );

and get back a reference to the li node that's scoped with customer

<li class="greet welcome" data-bind="scope: 'customer'">
    <!-- ko if: customer().fullname  -->
    <span data-bind="text: new String('Welcome, %1!').replace('%1', customer().firstname)">
    </span>
    <!-- /ko -->
    <!-- ko ifnot: customer().fullname  -->
    <span data-bind="html:'Default welcome msg!'"></span>
    <!-- /ko -->
</li>

You'll notice the use of theoretically and should. The problem, per this community forum thread, is there's been an undiscovered bug in the Magento_Ui/js/lib/knockout/extender/bound-nodes module lurking for a while now. It is not, as of the 2.1.x branch, code you can rely on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.