-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Proposal] Route helper for view #3402
Comments
I was gonna submit a question here if this is already implemented as I could not find anything on google. I've been using laravel so I know how helpful this route function is. I would love to see this on sails. |
@Mirago @RomainLanz I've taken a pass at implementing this a couple of times in the past, and I always run up against the issue of explicit routes vs implicit routes. It would be quite simple to make this work against explicit routes (i.e. in your Here's a proposal: Technical SummaryThis would be a method on the app object ( UsageCommon:e.g. sails.getRouteAddress({ target: 'PageController.about' });
// =>
// {
// method: 'get',
// url: '/about'
// } For non-GET routes:e.g. sails.getRouteAddress({ target: 'WolfController.howl' });
// =>
// {
// method: 'post',
// url: '/wolves'
// } For route addresses with no method specified:Method is returned as empty string; e.g. sails.getRouteAddress({ target: 'MeController.logout' });
// =>
// {
// method: '',
// url: '/logout'
// } URL pattern variables:e.g. sails.getRouteAddress({ target: 'PageController.projectDashboard' });
// =>
// {
// method: 'get',
// url: '/:organizationSlug/:username/projects/:projectSlug'
// } URL wildcard suffix:e.g. sails.getRouteAddress({ target: 'INodeController.upload' });
// =>
// {
// method: 'put',
// url: '/:username/tree/*'
// } Would that do the trick? |
What's the status on this? |
@niallobrien stewing in the old noggin at the moment-- was hoping to get some feedback on the proposal there. Whatcha think? |
@mikermcneil I'm liking it, though the view helpers would need to follow pretty quickly imo. @RomainLanz What's your opinion? |
Seems good to me. |
@RomainLanz @niallobrien K added here: https://github.com/balderdashy/sails/blob/master/ROADMAP.md#backlog It occurred to me that we still haven't clarified a few exit conditions:
Can you guys think of anything else we missed? btw @niallobrien re a view helper, the reason why I'm suggesting it isn't quite as big of a deal is that, in the current proposal, from your view, you'd be able to do: <a href="<%= sails.getRouteAddress({ target: 'PageController.about' }).url %>">About</a> Point being that, after looking at it, I'm not sure we really gain a whole lot from adding something like: <a href="<%= getRouteAddressUrl({ target: 'PageController.about' }) %>">About</a> (I feel like personally, I'd just forget which one was which all the time) |
By the way, just for the record, the new contribution guide I'm going to post this week would have us carrying out this discussion in a PR. Rather than disrupting everything in this issue and moving the discussion, I think it's best we just finish up in here. But just wanted to clarify that for anyone who runs across this and wonders to themselves "Hey, wait a minute!" -- and also to avoid making y'all feel awkward or anything. Anyways- we're good 👍 If you guys wouldn't mind reviewing the proposal and having a think about any other exit conditions or other stuff we might have missed and giving me a thumbs up, then I will take care of implementing this asap. |
Implementation-wise, it would be good to coordinate this with #2659 if we still want to include that as well. |
@RomainLanz @Mirago @niallobrien ok so after getting a start on implementing this, it seems to me that we'd be best off with two methods:
Does that check out with y'all? |
@RomainLanz @Mirago @niallobrien I'd also suggest that both methods support simplified usage as a shortcut, e.g.: As a shortcut for: var url = sails.getUrlFor({ target: 'DuckController.quack' }); you could write: var url = sails.getUrlFor('DuckController.quack'); |
… their implementation as specced #3402 (comment).
Stubbed implementation and added some basic tests here: 9992c37 Still needs:
If anyone would like to take a pass at submitting PRs for the last two bits, that'd be awesome, and I can review them tonight. |
Just ran across one more edge case:
|
Great work @mikermcneil - looking good! |
@niallobrien @RomainLanz just pushed up a version that should do the trick (as well as docs on the 0.12 branch). There are a few more tests I'd like to write, but this is usable now as specced here on master, afaik. One minor change is that, rather than returning null, if no matching route is found, we throw an error with code Feel free to pull down master and try it now if you're curious; otherwise we're going to push out a new rc with this feature and a few other improvements some time before Tuesday of this week. |
I'm going to go ahead and close this issue --- tests finished in c39cce2. Thanks for your help guys!(and please open a new issue if you notice any problems after you've had a chance to take this for a spin). |
Just a simple addition, that might be not that hard to add: what about supporting controllers without the "Controller" word, just like when you define 'Me.login' instead of 'MeController.login' |
@luislobo definitely- occurred to me to do that and I just hadn't gotten around to it yet. Would be glad to merge a PR on this if anyone has time to get to it. |
Hey @luislobo, I went ahead and added TODO stubs for this-- but it made me realize that the right way to do this is probably to make an addition to the hook spec. Not sure on the details yet, but it could be something like this:
The reason this is necessary (eventually at least) is that technically, the routing syntax for controllers/actions is not actually even part of the Sails router-- hooks just listen for the In short, I can't personally focus on enhancing this feature to support this for 0.12, but it'd be a great next step (and would allow for complete control over reverse routing in a future-proof way). |
@mikermcneil OK, thanks! I was already developing a solution for this, I'll check your additions and adjust accordingly |
… their implementation as specced balderdashy#3402 (comment).
… their implementation as specced balderdashy#3402 (comment).
@luislobo (sry just saw this) cool-- most of the time, you'll probably want http://sailsjs.org/documentation/reference/application/sails-get-url-for (but there's also |
Dear everyone,
Initial issue: #3151
Example
Additional Information
cc @slavafomin @tjwebb
The text was updated successfully, but these errors were encountered: