main page wait for page include to load

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Is there an event I can listen for or condition I can check from a primary page to know when a page include has finished loading?

I have a tabset where each tab contains a page include, and I am trying to run some code on the tabshow event, which needs to jQuery the contents of the page include of the active tab ( I want to make some modifications to the UI based on whether the tab contains a wizard). But the code in the 'tabshow' event is obviously running before the page include is finished loading. What's the best way to get the 'tabshow' code to wait for the page include to load?

Here's an excerpt of what we've got so far:
	
// Set tabshow function
$(tabSet).on('tabshow',function(event, data){
console.log(event);
console.log(data);
   // Save record into log for previous tab.
TimeOut = event.timestamp;
   saveProcessLog();
   // Start log entry for the new tab.
   TabName = data.tab.context.text;
   TimeIn = event.timestamp;
   // Check if current tab contains wizard and disable/enable buttons accordingly.
   console.log('Wizard exists before timeout: ' + !!$(data.panel).find('.nx-wizard').length);
   setTimeout(checkPanelForWizard(data.panel), 1000);
   /// 1000 is not long enough. Is there a way to wait for pageinclude load?
});
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Skuid fires a 'pageload' event on the page include's dom element. This event will bubble up all the way to your main window.  So you can attach a listener on any dom element that is a parent of the page include and listed for the event.

I use jQuery's "one" function instead of "on" because "one" will unregister the event handler once it's been fired once.

I would try this instead of your setTimeout...


data.panel.one('pageload',function(){
    console.log('page loaded!');
});
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Nice.

So, if the panel is loaded, I need to check for a wizard. If it's not loaded, I need to wait for it to load to check for the wizard. Does something like this make sense, or is there a better way?

if ($(data.panel).find('.nx-include-panel').attr('data-rendered') === "true"){
checkPanelForWizard(data.panel);
} else {
data.panel.one('pageload',function(){
console.log('page loaded!');
checkPanelForWizard(data.panel);
});
}
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
I don't think we have any supported APIs that will tell you if a page include component has been already loaded. If using .attr('data-rendered') is working for you, then you should be alright. I can't guarantee that it will always be there in a future release of Skuid, but I can't see a reason why it wouldn't.

One nitpick is that you don't need to wrap data.panel in the jQuery constructor since it's already a jQuery object.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Thanks, Ben.

Turns out the data-rendered attribute is "true" even if the page include isn't rendered. So I'm going with this:
    if (data.panel.find('.nx-include-panel').children().length) {
     checkPanelForWizard(data.panel);
   } else {
    data.panel.one('pageload',function(){
    console.log('include page loaded');
    checkPanelForWizard(data.panel);
});
   }