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

Optional parameters #3

Closed
jchaney opened this issue May 15, 2011 · 4 comments
Closed

Optional parameters #3

jchaney opened this issue May 15, 2011 · 4 comments

Comments

@jchaney
Copy link

jchaney commented May 15, 2011

This would help DRY out some code so that someone doesn't have to duplicate route maps.

For example ( this is how ExpressJS does it on the server side ):

Path.map("#/venues/:sortorder?").to(function{
    // Then we can do stuff like fn(this.params['sortorder'] || 'default');
});

I would happily fork it and submit the changes to make this work, however my javascript-fu is weak. Looking at the code, I couldn't tell how easily this could be accomplished. So this is just a feature request.

@mtrpcic
Copy link
Owner

mtrpcic commented May 16, 2011

Thank you for the suggestion, but this feature will not be implemented in PathJS. PathJS is meant to closely mimic the Rails router, which has specifically defined routes per action. After thinking on the issue, I've come up with a potential solution to your "default value" issue:

// Define your route like so:
Path.map("#/venues/:sort_order").to(function(){
    console.log(this.params["sort_order"]);
}).defaults({
    "sort_order": "ASC"
});

// Then, if you have a link like so:
<a href="#/venues/:sort_order">Venus</a>

// It will check the routes table for defaults, and change the active route to:
#/venues/ASC

This solution allows you to set defaults for any particular route, and access a route without having any data beforehand by using that default. Do you think this solution is a reasonable middle ground to keeping the strict "one route, one action" style of PathJS while providing you access to convenient defaults?

@jchaney
Copy link
Author

jchaney commented May 16, 2011

Actually, as of Rails 3 you can do optional parameters in your routes.

match 'posts(/new)', :to => 'posts#create'

Sends both of the below routes to the same controller.
/posts
/posts/new

I guess I just think that the below routes:
/venues
/venues/ASC
/venues/DESC

Looks more RESTful than:
/venues/default
/venues/ASC
/venues/DESC

But it really only applies in certain situations. One thing I should have mentioned is that in my original example, adding the '?' is what makes it optional, otherwise if the '?' was not added to the match, it would require that param to exist to match that route. So I think everyone wins in that scenario. Either way, I'm going to continue to use your lib, I think it's excellent. I just may have to duplicate a little more code. Thanks!

@mtrpcic
Copy link
Owner

mtrpcic commented Jun 3, 2011

This feature is under development.

@mtrpcic
Copy link
Owner

mtrpcic commented Jun 5, 2011

This feature was added as part of commit deb9cba60b7f32aa1d62, which is PathJS version 0.6. View the readme for details on how to implement these routes in your application.

@mtrpcic mtrpcic closed this as completed Jun 5, 2011
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