javascript - Meteor: getting route param on page loading -
here issue: trying make api call when loading page load 1 entity, saving few of entity attributes session - make 1 call, , use attributes whenever need them.
but when try id param route, router.current()
@ beginning of script, null.
on other hand, when call same router.current()
on helper
, can param.
any idea wrong / how can param before calling helpers
?
here html using 1 of attribute:
<input type="text" name="name" id="addcampaigninputname" value="{{currentcampaignname}}" placeholder="new campaign name">
and js:
if (meteor.isclient) { $(function () { console.log( router.current()); ==>> null if( router.current() !== null) { meteor.call("getapicampaignsbyid", router.current().params.id, function(error, results) { session.set('currentcampaigndetailsname', results.data.name); session.set('currentcampaigndetailstrackingmethod', results.data.tracking_method_id); session.set('currentcampaigndetailscampaigntypeid', results.data.campaign_type_id); }); } }); template.addcampaign.helpers({ currentcampaignname: function() { console.log( router.current()); ===>> dump data return session.get('currentcampaigndetailsname'); }, }); }
that's because template helpers run inside reactive computation, , router.current()
happens reactive data source, when it's ready helper rerun correct value.
since you're using iron:router
, use onbeforeaction
hook, because can tied specific route , executed when route parameters available (use this.params
).
router.route("/my-route", { name: "myroute", onbeforeaction: function(){ meteor.call("getapicampaignsbyid", this.params.id, function(error, results) { session.set('currentcampaigndetailsname', results.data.name); session.set('currentcampaigndetailstrackingmethod', results.data.tracking_method_id); session.set('currentcampaigndetailscampaigntypeid', results.data.campaign_type_id); }); // this.next(); } });
Comments
Post a Comment