I have Meteor.call method on template onCreated method, which basically gets the data from an api to load a dropdown control however when i route to other view and comeback it makes an api call again. How to avoid these sorry i couldn’t get much info on this.
Please help.
Sample code of my client onCreated and helpers
On my client,
Template.userForm.onCreated(function userFormOnCreated() {
const template = this;
template.loadCountry = new ReactiveVar(null);
Meteor.call(‘loadCountries’, function (err, resp) {
if (err) {
console.log(err);
} else {
template.loadCountry.set(resp.Countries);
}
});
});
Template.userForm.helpers({
countryList: function () {
return Template.instance().loadCountry.get();
},
});
That’s the expected behaviour, given you’ve bound the ReactiveVar
to the Template.onCreated
context. Whenever the template is created, a “fresh” ReactiveVar
is used. That means it’s not possible to guard-code around this, unless you move the ReactiveVar
into a common, higher-level context (the easiest one being window
):
Template.userForm.onCreated(function userFormOnCreated() {
if (! window.loadCountry) {
window.loadCountry = new ReactiveVar(null);
Meteor.call('loadCountries', function (err, resp) {
if (err) {
console.log(err);
} else {
window.loadCountry.set(resp.Countries);
}
});
});
Template.userForm.helpers({
countryList: function () {
return window.loadCountry.get();
},
});
1 Like
You can you Session to this case. It is reactive behavior too.
1 Like
Thanks very much for the approaches you guys shared. Much appreciated.
1 Like