Merge pull request #1509 from ianstormtaylor/optional-params

added optional groups to route syntax, with tests
This commit is contained in:
Jeremy Ashkenas
2012-10-02 19:43:42 -07:00
3 changed files with 33 additions and 5 deletions

View File

@@ -894,9 +894,10 @@
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var optionalParam = /\((.*?)\)/g;
var namedParam = /:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
var escapeRegExp = /[-{}[\]+?.,\\^$|#\s]/g;
// Set up all inheritable **Backbone.Router** properties and methods.
_.extend(Router.prototype, Events, {
@@ -947,6 +948,7 @@
// against the current location hash.
_routeToRegExp: function(route) {
route = route.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, '([^\/]+)')
.replace(splatParam, '(.*?)');
return new RegExp('^' + route + '$');

View File

@@ -1931,15 +1931,27 @@ var Workspace = Backbone.Router.extend({
similar to the <a href="#View">View</a>'s <a href="#View-delegateEvents">events hash</a>.
Routes can contain parameter parts, <tt>:param</tt>, which match a single URL
component between slashes; and splat parts <tt>*splat</tt>, which can match
any number of URL components.
any number of URL components. Part of a route can be made optional by
surrounding it in parentheses <tt>(/:optional)</tt>.
</p>
<p>
For example, a route of <tt>"search/:query/p:page"</tt> will match
a fragment of <tt>#search/obama/p2</tt>, passing <tt>"obama"</tt>
and <tt>"2"</tt> to the action. A route of <tt>"file/*path"</tt> will
match <tt>#file/nested/folder/file.txt</tt>,
passing <tt>"nested/folder/file.txt"</tt> to the action.
and <tt>"2"</tt> to the action.
</p>
<p>
A route of <tt>"file/*path"</tt> will match
<tt>#file/nested/folder/file.txt</tt>, passing
<tt>"nested/folder/file.txt"</tt> to the action.
</p>
<p>
A route of <tt>"docs/:section(/:subsection)"</tt> will match
<tt>#docs/faq</tt> and <tt>#docs/faq/installing</tt>, passing
<tt>"faq"</tt> to the action in the first case, and passing <tt>"faq"</tt>
and <tt>"installing"</tt> to the action in the second.
</p>
<p>

View File

@@ -69,6 +69,7 @@ $(document).ready(function() {
"contacts": "contacts",
"contacts/new": "newContact",
"contacts/:id": "loadContact",
"optional(/:item)": "optionalItem",
"splat/*args/end": "splat",
"*first/complex-:part/*rest": "complex",
":entity?*args": "query",
@@ -105,6 +106,10 @@ $(document).ready(function() {
this.contact = 'load';
},
optionalItem: function(arg){
this.arg = arg !== undefined ? arg : null;
},
splat : function(args) {
this.args = args;
},
@@ -199,6 +204,15 @@ $(document).ready(function() {
equal(router.args, 'long-list/of/splatted_99args');
});
test("routes (optional)", 2, function() {
location.replace('http://example.com#optional');
Backbone.history.checkUrl();
equal(router.arg, null);
location.replace('http://example.com#optional/thing');
Backbone.history.checkUrl();
equal(router.arg, 'thing');
});
test("routes (complex)", 3, function() {
location.replace('http://example.com#one/two/three/complex-part/four/five/six/seven');
Backbone.history.checkUrl();