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

Router add use object params option #3815

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ntgn81
Copy link

@ntgn81 ntgn81 commented Oct 2, 2015

Adding optional parameter to Router to make route callback handlers called with route params being an object instead of array.

#3799

@@ -1511,13 +1535,18 @@

// Convert a route string into a regular expression, suitable for matching
// against the current location hash.
_routeToRegExp: function(route) {
_routeToRegExp: function(route, paramNames) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akre54

I'm not so keen on modifying passed arguments. Let's bikeshed this decision a bit more (you may have been right in the first place).

I don't like this either, but this feels the least intrusive/most optimal since we are already RegExp parsing the input route string. And we can reuse namedParam and splatParam right where they are used.

Since _routeToRegExp() is only used from route(), how about moving it's body inside route() itself? Might be breaking change if someone is ovewritting _routeToRegExp() though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about moving it's body inside route() itself?

I'm concerned about taking a nice small chunk of reusable, functionally pure code and pulling it out of a method and into the body of an already big method.

I don't have a ton of time to work through this at the moment, but I should be freer next week. Try opening a pull and solicit feedback from other Backbone contributors. Looking forward to seeing this merged.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what express uses, so passing the array is probably fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this too. What if this method, instead of returning RegExp, returns an object {routeRegExp, paramNames}. It would keep it functionally pure.

@ntgn81
Copy link
Author

ntgn81 commented Oct 9, 2015

Any backbone guru have time to take a look at this and give me guidance on how to make this better?

Thanks!

if (_.isFunction(name)) {
callback = name;
name = '';
}
if (!callback) callback = this[name];

this._routeInfos = this._routeInfos || {};
this._routeInfos[route.toString()] = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#toString shouldn't be necessary, it'll be coerced by the object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will update. Thanks.

@ntgn81
Copy link
Author

ntgn81 commented Oct 28, 2015

Updated routed handler to receive (params, routeData) with tests.

Removed the route.toString in hash lookup too.

@jridgewell @akre54

@wlepinski
Copy link

Is this PR planned to land on master?

@jgonggrijp jgonggrijp added this to Low priority in Dusting off Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Dusting off
Low priority
Development

Successfully merging this pull request may close these issues.

None yet

5 participants