summaryrefslogtreecommitdiffstats
path: root/client/config/router.js
blob: ed9a069d63dd056690d37818cd24b7f0d5ca9f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// XXX Switch to Flow-Router?
var previousRoute;

Router.configure({
  loadingTemplate: 'spinner',
  notFoundTemplate: 'notfound',
  layoutTemplate: 'defaultLayout',

  onBeforeAction: function() {
    var options = this.route.options;

    var loggedIn = Tracker.nonreactive(function() {
      return !! Meteor.userId();
    });

    // Redirect logged in users to Boards view when they try to open Login or
    // signup views.
    if (loggedIn && options.redirectLoggedInUsers) {
      return this.redirect('Boards');
    }

    // Authenticated
    if (! loggedIn && options.authenticated) {
      return this.redirect('atSignIn');
    }

    Tracker.nonreactive(function() {
      if (! options.noEscapeActions &&
          ! (previousRoute && previousRoute.options.noEscapeActions))
      EscapeActions.executeAll();
    });

    previousRoute = this.route;

    this.next();
  }
});

// We want to execute our EscapeActions.executeLowerThan method any time the
// route is changed, but not if the stays the same but only the parameters
// change (eg when a user is navigation from a card A to a card B). This is why
// we can’t put this function in the above `onBeforeAction` that is being run
// too many times, instead we register a dependency only on the route name and
// use Tracker.autorun. The following paragraph explains the problem quite well:
// https://github.com/meteorhacks/flow-router#routercurrent-is-evil
// Tracker.autorun(function(computation) {
//   routeName.get();
//   if (! computation.firstRun) {
//     EscapeActions.executeLowerThan('inlinedForm');
//   }
// });