Search code examples
meteormeteor-blazeflow-router

Meteor & Flow Router: Marking Dynamically generated paths as 'active'


Needless to say, my experience with Meteor is lacking. I come from a Rails background, where you can do a lot more logic (and magic) in your views than Meteor.

The situation: I've got some routes that look like /things/:_id, and I've named that route 'thing' because it shows only one thing of a user's many owned things:

FlowRouter.route('/things/:_id', {
  name: 'thing',
     action() {
       BlazeLayout.render('appPage', {app: 'thing', sidebar: "thingsListOnThing", header: 'thingTitle'});
     }
});

As you can see, I'm also loading a template I've built to list all of the user's owned things on the sidebar for easy navigation. That template, thingsListOnThing is the target of this question.

Get the gist? I'm able to mark the route that dislays a template with a complete list of a user's things as active using zimme:active-route like so:

// A list of all a user's things
<div class="{{isActiveRoute name='things' class='is-active'}}">
List of Things
</div>

This package is great, but it won't help me with routes that look like /things/:_id because, then every link to each individual thing would be is-active on any thing page, not just the one where the current path's :_id matches the _id of the active thing.

I'm really kind of stuck here. My first guess would be to use template helpers, but I'm confused as to where to get started with that.

If need be, please as me to upload any piece of my code you require. I figured it's such a generic question that you guys probably don't need to see all of my code to understand what I'm trying to accomplish.


Solution

  • Using: zimme:active-route

    Template Helpers

    Template.listOfThings.helpers({
      activeListClass(page) {
        const active = ActiveRoute.name('thing') && FlowRouter.getParam('_id') === this._id;
        return active && 'active';
      },
    });
    

    Template HTML:

      <a class="{{isActivePath 'thing'}}" href="/things/{{_id}}">
          <div class="thingListItem {{activeListClass page}}">
          {{title}}
          </div>
      </a>