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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -