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

Render route without changing url or making a route "alias" or set "hidden" param ? #728

Open
hems opened this issue Nov 24, 2017 · 1 comment

Comments

@hems
Copy link

hems commented Nov 24, 2017

  • I have a route /social/:user_id and which renders a Blaze template which depends on FlowRouter.getParam('user_id').
  • I'm now on the process of making an alias to this route by using /:username instead of /social/:user_id

The problem here is that when I receive the username via URL, for instance mysite.com/britneyspears i have to do an _id lookup to find the user_id based on username

Once i fetch the user_id i would like to render my old route, /social/:user_id by doing something similar to:

FlowRouter.withReplaceState( => { FlowRouter.go( '/social/' + _id ) } )

But without changing the URL, this way the username only works as a "mask" to the end-user but the rest of my application can rely on

FlowRouter.getParam('user_id')

so it's actually the username "mask" is transparent for the client and for the backend side.

Other option would be to accept "username" instead of user_id in most places, but that would require the whole application to have some sort of "findIdByUsername" in a lot of places which would increase the number of queries my application do.

Another solution i tried was to set user_id param

Router.route( '/:username/', { action: =>
  Meteor.call( "find_id_by_username", username, ( error, _id ) => 
    FlowRouter.setParam( {user_id: _id })
    BlazeLayout.render 'layout', 'private' : 'social_user_id'
  )
})

But this won't work, because user_id param doesn't exist on the route FlowRouter doesn't allow me to set it! One solution would be to do something like:

...
    FlowRouter.set__Hidden__Param( {user_id: _id })
...

and then render, my template wouldn't even know what's happening!

    BlazeLayout.render 'layout', 'private' : 'social_user_id'

And if i add the 'user_id' to the route, for instance:

Router.route( '/:username/:user_id?', { action: =>
  Meteor.call( "find_id_by_username", username, ( error, _id ) => 
    FlowRouter.setParam( {user_id: my_user_id })
    BlazeLayout.render 'layout', 'private' : 'social_user_id'
  )
})

The route will change to /username/mongo_db_id which is very unfriendly to the user and won't allow me to do user-friendly routes like /:username/followers and so on.

What am I doing wrong? Would be much simpler for my application if I could just make the user_id work transparently so I won't have to deal with username everywhere.

@hems
Copy link
Author

hems commented Nov 24, 2017

One solution I found was to render BlazeLayout and sending user_id through the data parameter and then getting it on the layout through @data.user_id

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

1 participant