1

How to apply loader script to my code,

Loader : https://meetanshi.com/blog/magento-2-show-loader-till-ajax-call-not-completed/

My code:

define([
    'jquery',
    'Magento_Customer/js/model/authentication-popup',
    'Magento_Customer/js/customer-data',
    'Magento_Ui/js/modal/alert',
    'Magento_Ui/js/modal/confirm',
    'underscore',
    'jquery/ui',
    'mage/decorate',
    'mage/collapsible',
    'mage/cookies'
], function ($, authenticationPopup, customerData, alert, confirm, _) {
    'use strict';
    return function (widget) {

        $.widget('mage.sidebar', widget, {

            _updateItemQty: function (elem) {
                var itemId = elem.data('cart-item');

                var btnplus = elem.data('btn-plus');
                var btnminus = elem.data('btn-minus');

                this._ajax(this.options.url.update, {
                    'item_id': itemId,
                    'item_qty': $('#cart-item-' + itemId + '-qty').val(),
                    'item_btn_plus': btnplus,
                    'item_btn_minus': btnminus
                }, elem, this._updateItemQtyAfter);
                this._customerData();
            },

            /**
             * Update content after update qty
             *
             * @param {HTMLElement} elem
             */
            _updateItemQtyAfter: function (elem) {
                var productData = this._getProductById(Number(elem.data('cart-item')));

                if (!_.isUndefined(productData)) {
                    $(document).trigger('ajax:updateCartItemQty');

                    if (window.location.href === this.shoppingCartUrl) {
                        window.location.reload(false);
                    }
                }
                this._hideItemButton(elem);
            },

            _customerData : function ()  {
                var sections = ['cart'];
                customerData.invalidate(sections);
                customerData.reload(sections, true);
            }

        });
       
        return $.mage.sidebar;
        
    }
    
});

How to apply the loader?

2 Answers 2

0

You can show loader in multiple ways in Magento 2. Like:

First Way:

For Show Loader: $("body").trigger('processStart');

For Hide Loader: $("body").trigger('processStop');

Second Way: With the use of loader widget:

require(['jquery', 'loader'], function ($) {
    'use strict';
    $("body").loader("show");
    $("body").loader("hide");
});

Check-in details here: https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/widgets/widget_loader.html

0
0

Try processStart and processStop:

Show:

$('body').trigger('processStart');

Hide:

$('body').trigger('processStop');

Refer: https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/widgets/widget_loader.html

10
  • Thanks, i need to know how to apply in my script, can u pls help with my code?
    – Gem
    Apr 8, 2021 at 4:37
  • Have you tried to add $('body').trigger('processStart'); before this._ajax(.. and $('body').trigger('processStop'); after this._updateItemQtyAfter); ?
    – Saphal Jha
    Apr 8, 2021 at 4:53
  • Is my code right : codeshare.io/2BEYpm
    – Gem
    Apr 8, 2021 at 5:00
  • updated code : codeshare.io/ayeV39
    – Gem
    Apr 8, 2021 at 5:07
  • codeshare.io/GqJV9B
    – Saphal Jha
    Apr 8, 2021 at 5:14

Your Answer

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

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