You are viewing limited content. For full access, please sign in.

Question

Question

is there a way to check if all lookups are complete

asked on August 19, 2022 Show version history

I know about lookupcomplete, however it gets triggered with each lookup completing and I need a way to know when there are currently no other lookups running so I will only hide my loading html when all lookups are done. I can't do the event.triggerId 'trick' because many of lookups are based off the same trigger. 

I'm working on a paginated form and hiding/showing a custom loading html on every tab when look ups are running.  If I go to the last tab I can see the spinner next to the submit button well after my custom htmls are hidden. 

My code so far. "loadingText" is the class name that my custom HTMLs have 

  $(document).on("lookupcomplete", function (event) { 
    
   
    
    $('.loadingText').each(function () {
      $(this).hide();
    });   
    
  });
  

 

EDIT: I can't even check if fields are empty or not, because some lookups may end up not pulling data.

 

0 0

Answer

SELECTED ANSWER
replied on August 19, 2022

One option would be to use a counter field.

It's hard to predict the order in which lookups will complete and they're asynchronous, but JavaScript is not multi-threaded meaning you can update a variable without worrying about conflicts.

So, you could do something like this:

  1. Initialize a global JS "counter" variable set to 0 and a hidden "lookup complete" field on the form (default to true, false, or empty, depending on what you want to see before data entry).
  2. On lookup started, increment your counter and set "lookup complete" to false.
  3. On lookup completed decrement your counter and if the value is < 1 afterwards, set "complete" to true

 

You could tie that to another hidden variable on the form with field rules to show/hide your loading element.

For example, you could set it to Show when "lookup complete" is false, or Hide when it is true.

Alternatively, you could invert the boolean and make it a "lookup in progress" field; it just depends on the specific behavior you want/need.

1 0

Replies

replied on August 19, 2022

Is this for lookups that trigger in response to user actions, or as soon as the form loads?

1 0
replied on August 19, 2022

They trigger in response to the user entering data in a field. This triggers a lookup that fills a hidden field and then that hidden field has several lookups tied to it.

 

0 0
SELECTED ANSWER
replied on August 19, 2022

One option would be to use a counter field.

It's hard to predict the order in which lookups will complete and they're asynchronous, but JavaScript is not multi-threaded meaning you can update a variable without worrying about conflicts.

So, you could do something like this:

  1. Initialize a global JS "counter" variable set to 0 and a hidden "lookup complete" field on the form (default to true, false, or empty, depending on what you want to see before data entry).
  2. On lookup started, increment your counter and set "lookup complete" to false.
  3. On lookup completed decrement your counter and if the value is < 1 afterwards, set "complete" to true

 

You could tie that to another hidden variable on the form with field rules to show/hide your loading element.

For example, you could set it to Show when "lookup complete" is false, or Hide when it is true.

Alternatively, you could invert the boolean and make it a "lookup in progress" field; it just depends on the specific behavior you want/need.

1 0
replied on August 19, 2022

Great minds think alike.. this is what I ended up with before I got notice of your suggestion. Not exactly your suggestion but the same base idea. :) 

 

$(document).ready (function () {
  var lookupstartedCount = 0;
  var lookupcompleteCount = 0;


  $(document).on("lookupstarted", function (event) {
    //event.triggerId
    
    lookupstartedCount++;
    
    $('.loadingText').each(function () {
      $(this).show();
    });

  });


  $(document).on("lookupcomplete", function (event) { 
    
    lookupcompleteCount++;    

    
    if(lookupcompleteCount==lookupstartedCount)
    {
      $('.loadingText').each(function () {
        $(this).hide();
      });   
    };   

    
  });
}); /* END: $(document).ready (function () */

 

0 0
replied on August 19, 2022

The only issue you might have with that setup is that you could have the loading text show/hide multiple times as things run.

For example, because a lot of this is async it is going to be first come first serve meaning there's a possibility of a start/complete firing before all of the start events have been counted.

As a result, you could have multiple instances of the two values being equal; this may not be a concern, but it is worth mentioning because I've had issues before where the "loading" text would kind of flicker on/off for that reason.

 

Another thing to consider is if the user changes the value, which would trigger the process again so your counters would keep going up endlessly.

Again, this may not be a concern, but just something to consider.

1 0
replied on August 19, 2022

I ended up doing a hybrid of both of ours. I had already started resetting to 0 after hiding the html to avoid counters going up endlessly. But I like just having the one variable to deal with. :) 

 

$(document).ready (function () {
  var lookupCounter = 0;


  /* when a lookup starts */ 
  $(document).on("lookupstarted", function (event) {
    lookupCounter++;
    
    $('.loadingText').each(function () {
      $(this).show();
    });

  });

  $(document).on("lookupcomplete", function (event) { 
    
    lookupCounter--;

 
    if(lookupCounter<1)
    {
      $('.loadingText').each(function () {
        $(this).hide();
      });   
    };   

    
  });
}); /* END: $(document).ready (function () */

 

 

0 0
replied on April 16, 2023

A technical lead who used to work for Laserfiche showed me this method which I find works the best in my opinion.

I have a alert message that needs to display if there are no courses for an instructor to choose from after lookups complete.


 

// Display the alert message when no courses are available to choose.
// The instructor id field will be blank if there are no courses
function ShowMessagesOnComplete() {
  var elm = $('.instructor-id input').val();
  if (!elm) {
  	$('.error-message').show();
  }
}

// Hide the alert message initially
function HideMessagesOnStart() {    
    $('.error-message').hide();
}

//Create promise wrappers for page load events
function WaitForDocumentReady() {
    const dfd = $.Deferred();
    $(document).ready(() => {
        console.log('[DOM Ready] Fired');
        dfd.resolve();
    });
    return dfd.promise();
}

// Creates a promise that waits for the lookup handlers to be in place. 
// It does this by listening for the onloadlookupfinished event and resolving
// the promise when it is fired.
function WaitForOnLoadLookupFinished() {
    const dfd = $.Deferred();
    const onLookupFinishedHandler = () => {
        console.log('[On Load Lookup Finished] Fired');
        // Show the alert if no courses are available after lookup finished
        ShowMessagesOnComplete();
        dfd.resolve();
    };
    const interuptHandler = () => {
        console.log('[Cancel On Load Lookup] Fired');
        dfd.resolve();
    };

    $(document).one('onloadlookupfinished', onLookupFinishedHandler);
    $(document).one('cancelonloadlookupwait', interuptHandler);

    return dfd.promise();
}

// Initialize promises by calling the $.when() method, which waits for all promises
// to be resolved before executing the callback function.
$.when(
    WaitForOnLoadLookupFinished(),
    WaitForDocumentReady().then(() => { 
    	HideMessagesOnStart();
}));

 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.