4

I would like to challenge the template structure of Magento 2 loader which is declared in the lib/web/mage/loader.js but I can't find where this widget is calling by Magento to override the javascript ?

I mean, I'm able to extend the widget with this code :

define(['jquery','jquery/ui','mage/translate'], function ($) {
'use strict';

$.widget("custom.loader", $.mage.loader, {
    options: {
        icon: '',
        texts: {
            loaderText: $.mage.__('Please wait...'),
            imgAlt: $.mage.__('Loading...')
        },
        template:
            '<div class="loading-mask-custom" data-role="loader">' +
                '<div class="loader">' +
                    '<img alt="<%- data.texts.imgAlt %>" src="new path">' +
                    '<p><%- data.texts.loaderText %></p>' +
                '</div>' +
            '</div>'
    }
});

return $.custom.loader; });

And to call it from the Magento_Theme module in app/design but I still have the native one (loading-mask class)

1
  • I think you can override it with a custom css
    – fmsthird
    Commented May 22, 2019 at 3:12

1 Answer 1

2

Maybe it's late to reply, but I hope it can help someone who face same issue. Please create requirejs-config.js on your app/design/frontend/{Vendor}/{Custom_Theme}/Magento_Theme and add this code :

var config = {
map: {
    '*': {
        'loader' : 'Magento_Theme/js/custom-loader'
     }
   }
};

then create js file on inside Magento_Theme folder web/css/custom-loader.js and write your override loader.js template, here is mine as example :

define([
 'jquery',
 'jquery/ui',
 'mage/loader'
], function ($) {
'use strict';

$.widget('mage.loader', $.mage.loader, {
    options: {
        texts: {
            loaderText: $.mage.__('Please wait...')
        },
        template:
            '<div class="loading-mask" data-role="loader">' +
                '<div class="loader">' +
                    // change content with whatever you want
                    '<div class="spinner">' + 
                        '<div></div>' +
                    '</div>' +
                    // end change
                    '<p><%- data.texts.loaderText %></p>' +
                '</div>' +
            '</div>'
         }
     });
return $.mage.loader;
});

after finish create above files, deploy and refresh the cache of website :

php bin/magento c:c
php bin/magento c:f
php bin/magento setup:static-content:deploy

For testing: just hit this code on your browser console jQuery('body').loader('show') and you will see your custom loader.

1
  • 1
    Works like a charm, thank you. I'll just note that it would be better to use jquery-ui-modules/widget in the dependencies, since using jquery/ui means you'll be loading the entire jquery UI library on every page load which will slow down your site.
    – Lez
    Commented Jan 26 at 17:17

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.