The reason you're getting this "flashing" effect is probably because your router is implemented to be reactive (I'm not sure if this is a right strategy BTW) and since you're using Items.findOne
, this method invalidates the current computation as soon as the data requested by Meteor.subscribe
arrives to the Items
collection.
Also, note that every subscription within an active computation get's cancelled automatically as soon as the computation gets recomputed. However, as it is claimed in the documenation (look here) Meteor
should be smart enough to detect when you subscribe to the same data set twice, so this should not have any side effects.
If I were you, I would consider changing my router logic to something like this:
Session.set('currentItemId', id);
var status = Session.get('currentItemStatus');
if (status === 'ready')
return 'itemPage';
if (status === 'missing')
return 'sorryPage';
return 'loadingPage'; // probably status === 'loading'
And then, somewhere else in the project I would do:
Deps.autorun(function () {
Session.set('currentItemStatus', 'loading');
Meteor.subscribe('item', Session.get('currentItemId'), function () {
// onReady callback
var item = Items.findOne({_id:id});
if (item)
Session.set('currentItemStatus', 'ready');
else
Session.set('currentItemStatus', 'missing');
});
});
Please note, that if currentItemId
does not change, the computation defined Deps.autorun
will not be invalidated, so no unnecessary loadingPage
will be shown to the user.