1

The loader widget allows a developer to block page content while an AJAX request is ongoing. The devdocs describes it as:

The Loader widget blocks page content (all content or a part of it).

I'm having trouble figuring out how to block "a part of it."

By using showLoader: true in jQuery AJAX calls, you can have the widget automatically show and hide the loader through the lifetime of the request. Further, the loaderContext property is supposed to give the ability to have the loader appear in a specific area on the site - which seems like a good lead. (You can see an example in the reviews processing JS).

$.ajax({
  url: url,
  showLoader: true,
  loaderContext: $('.elem')
  // snip
)};

This doesn't work at all, in my experience – I just get a warning of Expected to start loader but did not find one in the dom when I use my own element there.

What gives? Do I need to make another instance of the Loader widget for every section I want it to hide?

2
  • I also facing same. Did you solve this problem?
    – Mahi M
    Aug 16, 2021 at 11:38
  • No, sorry. But I do know Magento 2 better since I asked this question! Looking at the JS code for lib/web/mage/loader.js, I can see that context only just calls trigger('processStart'); on the element passed to the setting. That shows me that you probably do have to instantiate a new Loader widget.
    – Leland
    Aug 17, 2021 at 12:17

1 Answer 1

0

try this, I have this working on my code and it seems to be the easiest way to have the loader going nicely

   $.ajax({
      url: url,
      beforeSend: function () {
         $('body').trigger('processStart');
      },
      success: function (res) {
         // snip 
         $('body').trigger('processStop');
       }
      )};
2
  • Not sure if you read the question - I want to have the loader activate on a specific part of the page. This would activate it on the body, which I do not want.
    – Leland
    Jul 30, 2020 at 20:10
  • 1
    I thought you were trying to freeze your screen, I overlooked the fact you wanted to freeze a specific part. I’ll have another go likely this weekend Jul 30, 2020 at 20:33

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.