How to use spinner with multiple enqueueActions












3















I have init function with multiple enqueueActions (to select data for different object). I want to start spinner, run all actions, stop spinner. Because all actions fire asynchronously what is the best way to toggle the spinner?
Example of one aciton:



var action = component.get('c.getSobjectsToAdd');
action.setCallback(this, function(a) {
if (a.getState() === "SUCCESS") {
var availableToAddObjects = a.getReturnValue();
component.set('v.availableToAddObjects',availableToAddObjects);
} else
if (a.getState() === "ERROR") {
helper.showToast(component, event, helper, 'Error', a.getError(), 'error');
}
});

$A.enqueueAction(action);









share|improve this question





























    3















    I have init function with multiple enqueueActions (to select data for different object). I want to start spinner, run all actions, stop spinner. Because all actions fire asynchronously what is the best way to toggle the spinner?
    Example of one aciton:



    var action = component.get('c.getSobjectsToAdd');
    action.setCallback(this, function(a) {
    if (a.getState() === "SUCCESS") {
    var availableToAddObjects = a.getReturnValue();
    component.set('v.availableToAddObjects',availableToAddObjects);
    } else
    if (a.getState() === "ERROR") {
    helper.showToast(component, event, helper, 'Error', a.getError(), 'error');
    }
    });

    $A.enqueueAction(action);









    share|improve this question



























      3












      3








      3


      1






      I have init function with multiple enqueueActions (to select data for different object). I want to start spinner, run all actions, stop spinner. Because all actions fire asynchronously what is the best way to toggle the spinner?
      Example of one aciton:



      var action = component.get('c.getSobjectsToAdd');
      action.setCallback(this, function(a) {
      if (a.getState() === "SUCCESS") {
      var availableToAddObjects = a.getReturnValue();
      component.set('v.availableToAddObjects',availableToAddObjects);
      } else
      if (a.getState() === "ERROR") {
      helper.showToast(component, event, helper, 'Error', a.getError(), 'error');
      }
      });

      $A.enqueueAction(action);









      share|improve this question
















      I have init function with multiple enqueueActions (to select data for different object). I want to start spinner, run all actions, stop spinner. Because all actions fire asynchronously what is the best way to toggle the spinner?
      Example of one aciton:



      var action = component.get('c.getSobjectsToAdd');
      action.setCallback(this, function(a) {
      if (a.getState() === "SUCCESS") {
      var availableToAddObjects = a.getReturnValue();
      component.set('v.availableToAddObjects',availableToAddObjects);
      } else
      if (a.getState() === "ERROR") {
      helper.showToast(component, event, helper, 'Error', a.getError(), 'error');
      }
      });

      $A.enqueueAction(action);






      apex lightning






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 at 17:47







      Eugene Vabishchevich

















      asked Jan 25 at 15:55









      Eugene VabishchevichEugene Vabishchevich

      187313




      187313






















          1 Answer
          1






          active

          oldest

          votes


















          6














          You just need to count the number of actions and remove the spinner when you're done. Here's my implementation, basically an in-flight counter:



          <aura:attribute name="actionCounter" type="Integer" default="0" />
          <aura:attribute name="showSpinner" type="Boolean" default="false" />
          <lightning:spinner class="{!v.showSpinner?'':'slds-hide'}" />




          // helper methods
          callServer: function(component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if(counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function(result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if(counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if(state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          }




          You would call the method from other helper methods like this:



          this.callServer(component, "c.doSomething", params, this.doSomethingResponse, this.doSomethingError);




          Example 2



          CONTROLLER



          ({
          doInit: function (component, event, helper) {
          helper.doInit(component);
          }
          })


          HELPER



          ({
          // helper methods
          callServer: function (component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if (counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function (result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if (counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if (state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          },
          doInit: function (component) {
          this.callServer(component, "c.getSobjectsToAdd", {}, this.getSobjectsToAddResult, this.showError);
          // Change this as appropriate //
          this.callServer(component, "c.otherMethod", {}, this.otherMethodResult, this.showError);
          },
          getSobjectsToAddResult: function (component, result) {
          component.set("v.availableToAddObjects", result);
          },
          otherMethodResult: function (component, result) {
          // ... not included here ... //
          },
          showError: function (component, error) {
          this.showToast(component, 'Error', error, 'error');
          },
          showToast: function (component, title, errorMessage, variant) {
          // ... not included here ... //
          }
          })





          share|improve this answer


























          • seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

            – Eugene Vabishchevich
            Jan 25 at 17:29











          • added example of action to the original post

            – Eugene Vabishchevich
            Jan 25 at 17:47











          • @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

            – sfdcfox
            Jan 25 at 18:16











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248012%2fhow-to-use-spinner-with-multiple-enqueueactions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          You just need to count the number of actions and remove the spinner when you're done. Here's my implementation, basically an in-flight counter:



          <aura:attribute name="actionCounter" type="Integer" default="0" />
          <aura:attribute name="showSpinner" type="Boolean" default="false" />
          <lightning:spinner class="{!v.showSpinner?'':'slds-hide'}" />




          // helper methods
          callServer: function(component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if(counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function(result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if(counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if(state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          }




          You would call the method from other helper methods like this:



          this.callServer(component, "c.doSomething", params, this.doSomethingResponse, this.doSomethingError);




          Example 2



          CONTROLLER



          ({
          doInit: function (component, event, helper) {
          helper.doInit(component);
          }
          })


          HELPER



          ({
          // helper methods
          callServer: function (component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if (counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function (result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if (counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if (state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          },
          doInit: function (component) {
          this.callServer(component, "c.getSobjectsToAdd", {}, this.getSobjectsToAddResult, this.showError);
          // Change this as appropriate //
          this.callServer(component, "c.otherMethod", {}, this.otherMethodResult, this.showError);
          },
          getSobjectsToAddResult: function (component, result) {
          component.set("v.availableToAddObjects", result);
          },
          otherMethodResult: function (component, result) {
          // ... not included here ... //
          },
          showError: function (component, error) {
          this.showToast(component, 'Error', error, 'error');
          },
          showToast: function (component, title, errorMessage, variant) {
          // ... not included here ... //
          }
          })





          share|improve this answer


























          • seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

            – Eugene Vabishchevich
            Jan 25 at 17:29











          • added example of action to the original post

            – Eugene Vabishchevich
            Jan 25 at 17:47











          • @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

            – sfdcfox
            Jan 25 at 18:16
















          6














          You just need to count the number of actions and remove the spinner when you're done. Here's my implementation, basically an in-flight counter:



          <aura:attribute name="actionCounter" type="Integer" default="0" />
          <aura:attribute name="showSpinner" type="Boolean" default="false" />
          <lightning:spinner class="{!v.showSpinner?'':'slds-hide'}" />




          // helper methods
          callServer: function(component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if(counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function(result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if(counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if(state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          }




          You would call the method from other helper methods like this:



          this.callServer(component, "c.doSomething", params, this.doSomethingResponse, this.doSomethingError);




          Example 2



          CONTROLLER



          ({
          doInit: function (component, event, helper) {
          helper.doInit(component);
          }
          })


          HELPER



          ({
          // helper methods
          callServer: function (component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if (counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function (result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if (counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if (state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          },
          doInit: function (component) {
          this.callServer(component, "c.getSobjectsToAdd", {}, this.getSobjectsToAddResult, this.showError);
          // Change this as appropriate //
          this.callServer(component, "c.otherMethod", {}, this.otherMethodResult, this.showError);
          },
          getSobjectsToAddResult: function (component, result) {
          component.set("v.availableToAddObjects", result);
          },
          otherMethodResult: function (component, result) {
          // ... not included here ... //
          },
          showError: function (component, error) {
          this.showToast(component, 'Error', error, 'error');
          },
          showToast: function (component, title, errorMessage, variant) {
          // ... not included here ... //
          }
          })





          share|improve this answer


























          • seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

            – Eugene Vabishchevich
            Jan 25 at 17:29











          • added example of action to the original post

            – Eugene Vabishchevich
            Jan 25 at 17:47











          • @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

            – sfdcfox
            Jan 25 at 18:16














          6












          6








          6







          You just need to count the number of actions and remove the spinner when you're done. Here's my implementation, basically an in-flight counter:



          <aura:attribute name="actionCounter" type="Integer" default="0" />
          <aura:attribute name="showSpinner" type="Boolean" default="false" />
          <lightning:spinner class="{!v.showSpinner?'':'slds-hide'}" />




          // helper methods
          callServer: function(component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if(counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function(result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if(counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if(state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          }




          You would call the method from other helper methods like this:



          this.callServer(component, "c.doSomething", params, this.doSomethingResponse, this.doSomethingError);




          Example 2



          CONTROLLER



          ({
          doInit: function (component, event, helper) {
          helper.doInit(component);
          }
          })


          HELPER



          ({
          // helper methods
          callServer: function (component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if (counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function (result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if (counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if (state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          },
          doInit: function (component) {
          this.callServer(component, "c.getSobjectsToAdd", {}, this.getSobjectsToAddResult, this.showError);
          // Change this as appropriate //
          this.callServer(component, "c.otherMethod", {}, this.otherMethodResult, this.showError);
          },
          getSobjectsToAddResult: function (component, result) {
          component.set("v.availableToAddObjects", result);
          },
          otherMethodResult: function (component, result) {
          // ... not included here ... //
          },
          showError: function (component, error) {
          this.showToast(component, 'Error', error, 'error');
          },
          showToast: function (component, title, errorMessage, variant) {
          // ... not included here ... //
          }
          })





          share|improve this answer















          You just need to count the number of actions and remove the spinner when you're done. Here's my implementation, basically an in-flight counter:



          <aura:attribute name="actionCounter" type="Integer" default="0" />
          <aura:attribute name="showSpinner" type="Boolean" default="false" />
          <lightning:spinner class="{!v.showSpinner?'':'slds-hide'}" />




          // helper methods
          callServer: function(component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if(counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function(result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if(counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if(state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          }




          You would call the method from other helper methods like this:



          this.callServer(component, "c.doSomething", params, this.doSomethingResponse, this.doSomethingError);




          Example 2



          CONTROLLER



          ({
          doInit: function (component, event, helper) {
          helper.doInit(component);
          }
          })


          HELPER



          ({
          // helper methods
          callServer: function (component, method, params, success, failure) {
          var action = component.get(method),
          counter = component.get("v.actionCounter") + 1;
          if (counter === 1) {
          component.set("v.actionCounter", counter);
          }
          action.setParams(params);
          action.setCallback(
          function (result) {
          var counter = component.get("v.actionCounter") - 1,
          state = component.getState();
          if (counter === 0) {
          component.set("v.showSpinner", false);
          }
          component.set("v.actionCounter", counter);
          if (state === "SUCCESS" || state === "DRAFT") {
          success(component, result.getReturnValue());
          } else {
          failure(component, result.getError());
          }
          }
          );
          $A.enqueueAction(action);
          },
          doInit: function (component) {
          this.callServer(component, "c.getSobjectsToAdd", {}, this.getSobjectsToAddResult, this.showError);
          // Change this as appropriate //
          this.callServer(component, "c.otherMethod", {}, this.otherMethodResult, this.showError);
          },
          getSobjectsToAddResult: function (component, result) {
          component.set("v.availableToAddObjects", result);
          },
          otherMethodResult: function (component, result) {
          // ... not included here ... //
          },
          showError: function (component, error) {
          this.showToast(component, 'Error', error, 'error');
          },
          showToast: function (component, title, errorMessage, variant) {
          // ... not included here ... //
          }
          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 25 at 18:15

























          answered Jan 25 at 16:29









          sfdcfoxsfdcfox

          260k12205450




          260k12205450













          • seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

            – Eugene Vabishchevich
            Jan 25 at 17:29











          • added example of action to the original post

            – Eugene Vabishchevich
            Jan 25 at 17:47











          • @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

            – sfdcfox
            Jan 25 at 18:16



















          • seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

            – Eugene Vabishchevich
            Jan 25 at 17:29











          • added example of action to the original post

            – Eugene Vabishchevich
            Jan 25 at 17:47











          • @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

            – sfdcfox
            Jan 25 at 18:16

















          seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

          – Eugene Vabishchevich
          Jan 25 at 17:29





          seems like my implementation is different or maybe wrong. I have init function and two enqueueAction one by one. Should I call callServer function in the beginning of each of my function? Below is an example of one my action:

          – Eugene Vabishchevich
          Jan 25 at 17:29













          added example of action to the original post

          – Eugene Vabishchevich
          Jan 25 at 17:47





          added example of action to the original post

          – Eugene Vabishchevich
          Jan 25 at 17:47













          @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

          – sfdcfox
          Jan 25 at 18:16





          @EugeneVabishchevich I've added a second expanded code section that demonstrates what your code would look like. Does this help?

          – sfdcfox
          Jan 25 at 18:16


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248012%2fhow-to-use-spinner-with-multiple-enqueueactions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter