Comments (6)
I thought about it as well, but since on RegExp the ?
is only referent to previous char or group than I thought it would be a better idea to have a separate token.
/foo? bar/; // matches "foo bar" and "fo bar"
/(foo?) bar/; // matches "foo bar" and "fo bar"
/(foo)? bar/; // matches "foo bar" and " bar"
note that the ?
is outside the group when the group is optional..
also see issue #33 (query string support), it will start getting too complex and will cause more confusions than it solves. BTW :
and {}
are invalid char in the middle of an URI (:
is reserved for the protocol/port) and {}
is forbidden without being escaped (RFC 3986)
from crossroads.js.
One more thing.. wouldn't it make sense to have a route always be a String in addition to which you could optionally add constraints. For example:
.addRoute('articles/{date}_{title}', { date: '\d{4}-\d{2}-\d{2}', title: '[\w_]+'});
.addRoute('products/{category}', { category: 'books|dvds'});
This way it shouldn't be a problem using optional parameters like this:
.addRoute('{controller}/{action}/{id?}')
Another convention could be:
// {id*} matches the rest of the path including / symbols
.addRoute('{controller}/{action}/{id*}')
You could also specify a delegate instead of regex for constraints:
.addRoute('products/{category}', { category: function(c) { $.ajax(..) or whatever } })
from crossroads.js.
Also I think users shouldn't worry about query strings and hashes (#bla-bla), if there are any routing tool should handle them for you automatically. For example:
.addRoute('{controller}/{action}', function(controller, action, queryString} { .. });
from crossroads.js.
you can kinda do what you want on the constraints:
var route = crossroads.addRoute('/articles/{date}_{title}');
route.rules = {
date : /\d{4}-\d{2}-\d{2}/,
title : /[\w_]+/
};
It allows RegExp, functions or an Array...
I find that the validation rules are less common than adding a listener to the route, that's why it isn't the second argument of addRoute()
.
also in some cases you may not need to validate one of the segments and/or you might need need to validate the string as a group, I decided that an object would be more flexible, see the example on the documentation for a complex use case.
There is also the option to pass the route as an RegExp instead of a String.
About query string and hashes there is already a feature request for it, see #33. (the first comment has an example on how to implement it on the current version), the problem with automagical approaches is that they are less flexible (eg, a query string inside the hash, order of arguments, etc...)
I created a new issue asking only for the {id*}
(#43) - it's easier to track individual issues than a single thread with lots of things..
from crossroads.js.
forgot to say, you have the option to set a route as an RegExp only because in some cases it might be easier and it doesn't affect crossroads complexity (in fact it's simpler since the patternLexer can skip some steps).
from crossroads.js.
I'm going to close this issue for now, please post feature requests as individual issues that way I can reference them in commits, create branches that implement each feature, etc.. I've been very busy lately so not sure when I will have time to implement new features (specially the ones I don't need at the moment). Will try to release v0.8.0 next month and it should include most of the stuff listed on the issue tracker right now. thanks for the feedback and features suggestion.
from crossroads.js.
Related Issues (20)
- routes with query params HOT 2
- How to use routing libraries like crossroads js with preload js?
- Non-encoded slashes (/) on query params choosing wrong pattern
- Why do `setting window.location.hash` and `hasher.setHash` result in different behavior of question mark `%3F`?
- URL param names are lost : route handler should get param object with names as fields
- Route Losing Reference After First Call HOT 1
- Best way to detect param changes in the same route
- Bower update failing HOT 3
- test
- Support of native URLs HOT 2
- Support of wildcard in the beginning of the URL HOT 2
- destroy function HOT 1
- Getting params as one object not on callback arguments HOT 1
- Is this project still alive? HOT 5
- Trailing slashes are appended to routes that have query params HOT 1
- Empty url in rules
- Crossroads - 404 on route HOT 1
- Nested routes
- [ SUGGESTION ] .routed.addBefore() and .routed.addAfter()
- Unmatched routes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crossroads.js.