mokk
mokk

Reputation: 916

Meteor: getting route param on page loading

Here is my issue: I am trying to make an API call when loading my page to load one entity, saving few of this entity attributes into the session - so I basically make one call, and use the attributes whenever I need them.

But when I try to get the id param from the Route, the Router.current() at the beginning of my script, it is null.

On the other hand, when I call the same Router.current() on a helper, I can get my param.

Any idea what is wrong / how can I get this param before calling my helpers ?

Here is my html where I will be using one of this attribute:

<input type="text" name="name" id="addCampaignInputName" value="{{currentCampaignName}}" placeholder="New Campaign Name">

And the 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');
     },
   });
 }

Upvotes: 2

Views: 55

Answers (3)

saimeunt
saimeunt

Reputation: 22696

That's because template helpers are always run inside reactive computation, and Router.current() happens to be a reactive data source, so when it's ready the helper will rerun with the correct value.

Since you're using iron:router, you could use an onBeforeAction hook, because they can be tied to a specific route and are executed when route parameters are 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();
  }
});

Upvotes: 3

Michael Paddock
Michael Paddock

Reputation: 260

Your first function is likely running on the page before anything else has loaded, and it doesn't know what Router is. The best way to set these before the template helpers are loaded is to use iron-router's onBeforeAction hook:

Router.route('/', {
  name: 'home',
  onBeforeAction: function() {
    //set your session variables here
    this.next();
  }
});

This way, the router should load the page with your session vars already set.

Upvotes: 1

Billybobbonnet
Billybobbonnet

Reputation: 3226

You should load your template only once its subscriptions/parameters are ready. Look at my answer here, maybe it will solve your problem (especially the part where you attach your parameters to your template data object.)

Upvotes: 1

Related Questions