Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Redirect from groups? #182

Closed
peterchoo opened this issue Jun 30, 2015 · 6 comments
Closed

[Question] Redirect from groups? #182

peterchoo opened this issue Jun 30, 2015 · 6 comments
Assignees

Comments

@peterchoo
Copy link

I've been looking at using groups to organise my routes, and one feature which appears to be missing from the groups is the ability to redirect to a route if the URL defined by the group is hit.

Consider the following pages:

  • User list
  • User edit
  • Organisation edit

All of these pages are to be housed under the /admin section of the application. Using just routes I have done the following:

Router.route('/admin', {
  name: 'admin',
  action: function() {
    Router.go('admin.users');
  }
});

Router.route('/admin/users', {
  name: 'admin.users',
  ...
});

Router.route('/admin/users/:userId', {
  name: 'admin.users.edit',
  ...
});

Router.route('/admin/organisation', {
  name: 'admin.organisation',
  ...
});

Using a group, if I navigate to /admin I can't find a way of redirecting to /admin/users.

Is this a feature which could be added, or should I carry on defining my routes as I have been?

Thanks

@arunoda
Copy link
Contributor

arunoda commented Jun 30, 2015

I think you can do the same with groups. Create a group for /admin and create / route inside to do the redirect. I think it should be FlowRouter not just Router.

var adminRoutes = FlowRouter.group({
  prefix: '/admin'
});

adminRoutes.route('/', {
  triggersEnter: [function(contex, redirect) {
    redirect('/admin/users');
  }];
});

adminRoutes.route('/users', {
  action: function() {
    console.log("Yeah, I'm on /users");
  }
});

@peterchoo
Copy link
Author

Hi,

Thanks for the reply, that will indeed work for me. The Router vs FlowRouter was purely haste on my part.

I have a suggestion: Could we pass in self.go on both https://github.com/meteorhacks/flow-router/blob/02758cb1411ce2705c8729fee61d369e1f255134/client/router.js#L85 and #L96 ? This would allow us to pass in a route name as well as a url.

As another observation, can the "already redirected" Error be triggered? Wouldn't the return on #L82 exit the runTriggers function?

@peterchoo
Copy link
Author

I've had a little play:

Changing the runTriggers calls in client/router.js to use a bound call to Router.go() does let you use route names.

    Triggers.runTriggers(
      triggers,
      self._current,
      _.bind(self.go, self),
      afterAllTriggersRan
    );
Admin = FlowRouter.group({
  prefix: '/admin'
});

Admin.route('/', {
  triggersEnter: [function(context, redirect) {
    redirect('admin.users');
  }]
});

I haven't done any in depth testing, can you think of nay reason you wouldn't want to allow route names in this situation?

Thanks for your time.

@arunoda
Copy link
Contributor

arunoda commented Jul 2, 2015

Okay. I got it. Let's see what we can do here.

@arunoda arunoda added the doing label Jul 2, 2015
@arunoda arunoda self-assigned this Jul 2, 2015
@arunoda
Copy link
Contributor

arunoda commented Jul 2, 2015

Implemented this and release with 1.17.2

@peterchoo
Copy link
Author

👍 Thanks for your fast response! Updating now!

@arunoda arunoda closed this as completed Jul 8, 2015
@arunoda arunoda removed the doing label Jul 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants