/   /   /  Magento 2 Components some js process details(mage.js,apply.js)

Note:

For more extensions and themes visit our store

Magento 2 Components some js process details(mage.js,apply.js)


M2 system use  for read and execute (scriptSelector = 'script[type="text/x-magento-init"]' and dataAttr = 'data-mage-init' )  many js file like vendor/magento/magento2-base/lib/web/mage/apply/main.js  and scripts.js and vendor/magento/magento2-base/lib/web/mage /mage.js
you can find vendor/magento/magento2-base/lib/web/mage 
in normal case  you can declare as virtual :

  1.  
  2.         <script type="text/x-magento-init"> 
  3.         { 
  4.             "[data-role=tocart-form], .form.map.checkout": { 
  5.                 "catalogAddToCart": {} 
  6.             } 
  7.         }
  8.        </script>
  9.  

or simple :
  1.  
  2. data-mage-init='{"catalogAddToCart": {}}'
  3.  

vendor/magento/magento2-base/lib/web/mage/apply/scripts.js  is responsible for parses 'script' tags with a custom type attribute and moves it's data
     * to a 'data-mage-init' attribute of an elemennt found by provided selector.
     * Note: All found script nodes will be removed from DOM

and mage.js take a hand for  plugin mage, initialize components on elements  :

  1.  
  2.     $.fn.mage = function (name, config) {
  3.         config = config || {};
  4.         this.each(function (index, el) {
  5.             mage.applyFor(el, config, name);
  6.         });
  7.       return this;
  8.     };
  9.  

Here mage.js iterate all element in dom was declared (scriptSelector = 'script[type="text/x-magento-init"]' and dataAttr = 'data-mage-init' ) applied in it . For that it use mage.applyFor(el, config, name) ,

the name in our example is :  catalogAddToCart Components path  in declared inside requirejs-config.js 
config : in our case we passed any configuration you can pass template variable in general or sub child component .

 in bottom we Init components inside of dynamically updated elements:

  1.  
  2.     /**
  3.      * Init components inside of dynamically updated elements
  4.      */
  5.     $('body').on('contentUpdated', function () {
  6.         if (mage) {
  7.             mage.apply();
  8.         }
  9.     });
  10.  

we have here 2 functions apply and applyFor is inside vendor/magento/magento2-base/lib/web/mage/apply/main.js  ,ok applyFor is equal init function 
 
  1.  
  2.   /**
  3.      * Initializes components assigned to a specified element via data-* attribute.
  4.      *
  5.      * @param {HTMLElement} el - Element to initialize components with.
  6.      * @param {Object|String} config - Initial components' config.
  7.      * @param {String} component - Components' path.
  8.      */
  9.     function init(el, config, component) {
  10.         require([component], function (fn) {
  11.             if (typeof fn === 'object') {
  12.                 fn = fn[component].bind(fn);
  13.             }
  14.             if (_.isFunction(fn)) {
  15.                 fn(config, el);
  16.             } else if ($(el)[component]) {
  17.                 $(el)[component](config);
  18.             }
  19.         });
  20.     }
  21.  

and apply Initializes components assigned to HTML elements via [data-mage-init]. 

*      @example Sample 'data-mage-init' declaration.
*      data-mage-init='{"path/to/component": {"foo": "bar"}}'

it call the function init many time :

  1.  
  2.         apply: function () {
  3.             var virtuals = processScripts(),
  4.                 nodes = document.querySelectorAll(nodeSelector);
  5.             _.toArray(nodes)
  6.                 .map(getData)
  7.                 .concat(virtuals)
  8.                 .forEach(function (itemContainer) {
  9.                     var element = itemContainer.el;
  10.                      _.each(itemContainer.data, function (obj, key) {
  11.                             if (obj.mixins) {
  12.                                 require(obj.mixins, function () {
  13.                                     for (var i = 0, len = arguments.length; i < len; i++) {
  14.                                         $.extend(true, itemContainer.data[key], arguments[i](itemContainer.data[key], element));
  15.                                     }
  16.                                    delete obj.mixins;
  17.                                     init.call(null, element, obj, key);
  18.                                 });
  19.                             } else {
  20.                                 init.call(null, element, obj, key);
  21.                             }
  22.                         }
  23.                     );
  24.               });
  25.         },
  26.         applyFor: init
  27.     };
  28.  

that is general view of component ...

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.