We’re using Meteor.autorun to look for changes in a ‘filters’ Session variable, make two asynchronous calls to the server for the filtered data and update two ‘list’ Session variables with the results. In another .autorun function, the script waits for changes in the list Session variables, and then turns on applicable templates.
So,
Meteor.autorun(function(){
set_search_session(Session.get('filters'));
render_templates(
Session.get('places'),
Session.get('awards')
);
});
var set_search_session = function(filters) {
Meteor.call('search_places', filters.places, function(error, data) {
Session.set('places', data);
};
Meteor.call('search_awards', filters.awards, function(error, data) {
Session.set('awards', data);
};
};
var render_templates = function(places, awards) {
var filters = Session.get('filters');
if (!awards && _.isUndefined(filters['neighborhood'])) {
Session.set('template', 'place_detail');
};
};
The problem is that the render_templates function is run twice, as it is apparently still dependent on Session.get(‘filters’). So in any autorun function, it looks like you’re unable use a Session.get() function that is separate from the one you’re observing changes on.
Is there a way around this?
Thanks very much for your help.
There are two separate reasons that changing
filterscallsrender_templates. One is thatrender_templatesis called within the same autorun as aSession.get('filters'); the other is thatrender_templatesitself callsSession.get('filters').To fix the former, split the autorun into two separate autoruns:
To fix the latter, perhaps move the “neighborhoods” field out of
Session.get('filters')into its own Session field?