pablocattaneo
5/23/2017 - 5:54 PM

Load script javascript that is no a library in Magento 2

Load script javascript that is no a library in Magento 2

1- Registramos los js en requierejs-config.js, en este ejemplo "carrefour.owl"

var config = {
   paths: {
        'owlcarousel': 'TopGroup_ListadoProductos/js/owl.carousel.min',
        'carrefourowljs': 'TopGroup_ListadoProductos/js/carrefour.owl'
   },   
   shim: {
       'owlcarousel': {
          deps: ['jquery']
       },
       'carrefourowljs':{
          deps: ['owlcarousel']
       }
   }
};

2- En el script a ejecutar debemos cargar las dependencias en detro de require(["dependencia_1", "dependencia_1"])

require([
  "jquery",
  "owlcarousel"
], function ($, owlcarousel) {
  $(document).ready(function () {
    
   /* Your code here*/

  }); /* document ready */

});

3 - Inicializar el script en el template

<div data-mage-init='{"name_que_usamos_en_el_path":{}}'></div>
En nuestro ejemplo:
<div data-mage-init='{"carrefourowljs":{}}'></div>

4 - Delete requirejs-config.js de pub
rm pub/static/_requirejs/frontend/Vendor_name/Theme_name/[languaje]/requirejs-config.js

Example: rm pub/static/_requirejs/frontend/TopGroup/carrefour/es_AR/requirejs-config.js

5 Deploy js

6 Clean cache
 php bin/magento cache:clean