Search code examples
meteoriron-routerspacebars

Spacebars-generated dynamic links do not trigger Iron Router in Meteor?


I've got this in my routes:

Router.route('/videos/:id', {
  name: 'VideoPage',
  data: function(){
    return Videos.findOne(this.params.id);
  }
})

This template shows up at the route above:

Template.VideoPage.helpers({

 'videoIds': function(){

    var myVideoIds = [
        "23456",
        "h5e45t",
        "f4e4w",
        "g6h4h"
    ];

    return myVideoIds;

  }

});

HTML:

<template name="VideoPage">

{{videoTitle}}

<p>Click the links below to get to a new video page</p>

{{#each videoIds}}

    <a href="/videos/" + {{this}}>

{{/each}}

</template>

When I click on a link, the URL in the browser changes from something like /videos/23456 to something like /videos/f4e4w, but the request never actually goes through Iron Router and the Router.route function. I put a debugger before Router.route and it triggers on initial page load but does NOT trigger when the links are clicked.

I understand that Iron Router's default behavior is NOT to re-render a template the user is currently on, but this is the same template with different url params that are used to change the data in the template, so IMO it should still re-render.


Solution

  • Ok, false alarm. It appears that Router.route DOES fire and updates the data context every time the params._id changes. I was placing the debugger outside of the Route function when I should have been placing it inside of the data context function.

    The example that I gave in this answer was also a highy, highly simplified example. It wasn't working in my real-life project due to something else (a complex video iframe generator) that was being refreshed improperly.

    But rest assured that going from /videos/f4e4w to /videos/23456 by clicking a link DOES still go through Iron Router and the params does get read and the data does get updated.