3

I've got a Meteor project which uses iron-router.

In my project I have a bootstrap navbar and unfortunately whenever I navigate to a route while the navbar is expanded (due to the screen being min width), the navbar remains expanded when I go to a new route.

Is there a way to set the navbar to collapse again upon changing routes like it would with a usual page refresh?

Navbar Template:

<template name="navigation">
<nav class="navbar navbar-default">
<div class="container-fluid">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="{{pathFor route='home'}}"><span class="glyphicon glyphicon-home"></span>&nbsp;&nbsp;&nbsp;&nbsp;RNG Leaderboard</a>
  </div>

  <div class="navbar-collapse collapse" id="navbar-collapse" aria-expanded="false" style="height: 1px;">
    <ul class="nav navbar-nav">
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-phone"></span>&nbsp;&nbsp;&nbsp;&nbsp;Games <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li>
            <a href="/headsortails">Heads or Tails</a>
          </li>
        </ul>
      </li>
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-stats"></span>&nbsp;&nbsp;&nbsp;&nbsp;Leaderboards <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li>
            <a href="/headsortailsleaderboard">Heads or Tails</a>
          </li>
        </ul>
      </li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
      {{#if currentUser}}
        {{#if isInRole 'admin'}}
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-cog"></span>&nbsp;&nbsp;&nbsp;&nbsp;Maintenance <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li>
                <a href="/manageusers"><span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;&nbsp;&nbsp;Manage Users</a>
              </li>
            </ul>
          </li>
        {{/if}}
        <li><a href="/@{{currentUser.username}}"><span class="glyphicon glyphicon-user"></span>&nbsp;&nbsp;&nbsp;&nbsp;{{currentUser.profile.firstName}}&nbsp;{{currentUser.profile.lastName}}</a></li>
        <li><a id="logout" href="{{pathFor route='login'}}"><span class="glyphicon glyphicon-log-out"></span>&nbsp;&nbsp;&nbsp;&nbsp;Log out</a></li>
      {{else}}
        <li><a href="{{pathFor route='login'}}"><span class="glyphicon glyphicon-log-in"></span>&nbsp;&nbsp;&nbsp;&nbsp;Log in</a></li>
        <li><a href="{{pathFor route='signup'}}"><span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;&nbsp;&nbsp;Sign up</a></li>
      {{/if}}
    </ul>
  </div>
</div>
</nav>
</template>

Router code:

Router.configure({
  layoutTemplate: 'layout', // Defines the layout template
  loadingTemplate: 'loading', // Defines the loading template
  onAfterAction: function(){
    $('.nav-collapse').collapse('hide');
  }
});

Router.plugin('dataNotFound', {
  notFoundTemplate: 'dataNotFound'
});

Router.route('/', {
  name: 'home',
  template: 'home'
});

Router.route('/signup');
Router.route('/login');
Router.route('/recoverpassword');
Router.route('/resetpassword');

Router.route('/manageusers', {
  name: 'manageusers',
  controller: 'ManageUsersController',
});

ManageUsersController = RouteController.extend({
  template: 'manageusers',
  before: function() {
    if (!Roles.userIsInRole(Meteor.userId(), 'admin')) {
      this.redirect('home');
    } else {
      this.render('manageusers');
    }
  }
});

var usernameRoute = '/@:username';
Router.route(usernameRoute, {
  name: 'profile',
  controller: 'ProfileController'
});

ProfileController = RouteController.extend({
  template: 'profile',
  waitOn: function() {
    return Meteor.subscribe('userProfile', this.params.username);
  },
  data: function() {
    var username = Router.current().params.username;
    return Meteor.users.findOne({
      username: username
    });
  }
});

Router.route('/headsortails');
Router.route('/headsortailsleaderboard');
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

3 Answers3

1

An inelegant but practical solution is to check if the navbar is expanded THEN click on it - as per Barry Doyle's solution

  closeNavBar: function() {
    var isExpanded = $('.navbar-toggle').attr('aria-expanded') === true;
    if(isExpanded) {
      $('.navbar-toggle').click();
    }
    this.next();
  },
pashpops
  • 50
  • 4
0

Based off of Michel's answer and some digging around I came up with the following solution in my router.js file:

Router.configure({
  onAfterAction: function(){
    $('.navbar-toggle').click();
  }
});
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • Are you sure this works? When the navbar is closed and I go to a new route, the route open automatically which is very annoying. – Amir Rahbaran Jun 26 '16 at 18:56
  • Ah, didn't consider that, in my application you could only go to a new route via the navbar, so you'll need to do some additional code to prevent this function from running in situations where you go to a new route using a different method. – Barry Michael Doyle Jul 04 '16 at 10:31
0

Neigher both provided solutions worked.

It has to be like this. attr() returns string 'true' or 'false' not boolean, that's why use === 'true'.

Router.configure({
    ...
    onAfterAction() {
        const navbarToggle = $('.navbar-toggle');
        if (navbarToggle.attr('aria-expanded') === 'true') {
            navbarToggle.click();
        }
    }
});
Jirik
  • 1,435
  • 11
  • 18