I want to create a preloading script that performs a number of async functions to download external content. I’m pretty close here, but I haven’t quite figured out how to to defer calling this.next() in my onBeforeRun function. In the code below you can see I use a loop and setTimeout but I lose the context of my router somehow and this.next() is undefined.
if (Meteor.isClient) {
IR_BeforeHooks = {
preloadProject: function() {
var itemsProcessed = 0;
_.each(items.items, function(e) {
HTTP.get(e.S3URL, {
headers: {
'Accept': '*/*'
},
responseType: 'arraybuffer' //requires aldeed:http
}, function(error, result) {
if (error) {
Session.set('error', {
'title': 'Could not download',
'message': error
});
}
if (result) {
itemsProcessed = itemsProcessed + 1;
}
}) //http get
}) //each
function waitToRender(router) {
console.log('waiting...')
var progress = (itemsProcessed / items.items.length) * 100;
if (progress < 100) {
$('.progress-bar').css('width', Math.floor(progress) + '%');
setTimeout((function() {
waitToRender(router);
}), 50);
//console.log('timeout for a few ms here')
}
else {
console.log('all done, render');
router.next(); // I get this is not a function error here
}
}
waitToRender(this);
}
}
}
and my router
Router.before(
IR_BeforeHooks.preloadProject,
{
only:['editor', 'viewer', 'embedder']
}
);
So my questions are:
How can I maintain the context of my router inside of my WaitToRender function so I can call this.next() inside of it?
or
Am I doing this completely wrong / is there a much better way?
Thanks anybody who has a moment to look at this