Page load timing: helpful wrapper
This is a handy script for when you have trouble getting your code to execute after a view/ views have rendered - maybe you're retrieving values from 2 or more tables but it's firing before the one can finish loading /This works most times that something's not loaded in time to retrieve a value you need from it. This wrapper let's you specify the views to wait for and only executes at that point. If you're having timing issues with a plugin or jquery you may be able to just replace the view names and paste your code in the middle.
waitForAllEvents([
'knack-view-render.view_xxx', //change as necessary
'knack-view-render.view_yyy //change as necessary
], function(){
//YOUR CODE HERE
})
function waitForAllEvents(listOfEvents, cb){ //cb is a callback
var triggeredEvents = []; //create an array called triggered events
listOfEvents.forEach(function(eventKey){ //Looping through list of events
var listener = function(event, view, record){ //creating event listener
// $(document).off(eventKey, listener);
// removing that listener from the document
// this is for Safety / optimization (in case the page loads twice)
triggeredEvents.push(eventKey); //add event to list of already triggered events
if(triggeredEvents.length === listOfEvents.length){ //have all the events finished?
cb() //run the callback
}
}
$(document).on(eventKey, listener);
})
}