Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'scope' keyword in Knockout 'data-bind'

I found the following code in Magento 2 HTML:

<div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'">

Magento 2 uses Knockout on frontend but I cannot found in Knockout docs what does keyword "scope" mean in this context. Is it a Magento 2 feature?

like image 574
Alex Gusev Avatar asked Oct 24 '25 23:10

Alex Gusev


1 Answers

Your assumption is right, the scope binding is not build-in into knockout, but a magento feature.

From what I understood, magento uses the applyBindings function from knockout without assigning a viewmodel. The scope binding then looks for and loads a registered viewmodel (in this case: 'block-totals') and applies this to the DOM node, where you have your scope binding.

For detailed information, have a look here (and give the guy some credit, too :))

https://magento.stackexchange.com/questions/120447/how-does-magento-2-apply-knockoutjs-bindings

http://alanstorm.com/magento_2_knockoutjs_integration/

like image 164
chris vietor Avatar answered Oct 26 '25 13:10

chris vietor