I would like to know how I could force loading template to have a minimum load time (ex: 3 seconds) before loading my default template / controller (App Controller).
Is that a way to do that using waitOn ? Thank you!
I found this solution on stack overflow but in my case I have no subscriptions, so the bellow is not valid, please check my comments on the code bellow to see where I’m getting lost.
Router.configure({
layoutTemplate: 'AppController',
loadingTemplate: 'loading',
waitOn: function() {
//var sub = Meteor.subscribe("something");
var dep = new Tracker.Dependency();
var isSubReady = false;
var hasWaitTimePassed = false;
var waitFor = 1000; // Wait for 1 sec
Meteor.setTimeout(function() {
hasWaitTimePassed = true;
dep.changed();
}, waitFor);
Tracker.autorun(function() {
//isSubReady = sub.ready();
dep.changed();
});
return {
ready: function() {
dep.depend();
return hasWaitTimePassed;
//&& isSubReady;
},
stop: function() {
// What should I stop here?
//sub.stop();
}
};
waitOn: function() {
var hasWaitTimePassed = new ReactiveVar(false);
var waitFor = 3000; // Wait for 3 sec
var stopped = false;
Meteor.setTimeout(function() {
if (!stopped) {
hasWaitTimePassed.set(true);
}
}, waitFor);
return {
ready: function() {
// hasWaitTimePassed is a reactive source, so this should do just fine
return hasWaitTimePassed.get();
},
stop: function() {
// Stopping should prevent the hasWaitTimePassed becoming true.
stopped = true;
}
};
}
Hmm, I think the problem might be that waitOn itself is reactive, and starts over when hasWaitTimePassed changes. This is one of the main drawbacks with iron:router as opposed to flow-router, everything is reactive, which can cause problems.
I’m not on a computer to test all these things, so I’m kind of shooting blind here, but try this:
I tried your code but the issue is still the same… loadingTemplate is loaded by not rendered after 3 seconds
Please check my code bellow and my comments to see if you can help me to find what I am doing wrong.
I also deployed this version so you can check my console.log() http://ns1-timeout.meteor.com/
Thank you very much
Router.configure({
layoutTemplate: 'appLayout',
loadingTemplate: 'loading',
waitOn: function () {
var isTimePassed = false;
var clock = 3;
var timeLeft = function() {
if (clock > 0) {
clock--;
Session.set("time", clock);
console.log(clock);
} else {
console.log("That's All");
//return true
isTimePassed = true;
Meteor.clearInterval(interval);
console.log('is Time passed: '+ isTimePassed);
return isTimePassed; // seems it is being ignored
}
};
var interval = Meteor.setInterval(timeLeft, 1000);
return {
ready: function () {
console.log('return ready: ' + isTimePassed);
return isTimePassed; // keeps the loading page and does not redirect to applayout if changed to false, loadingTemplate is not loaded and
}
}
}
});