Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  34] [ 4]  / answers: 1 / hits: 24185  / 12 Years ago, wed, march 7, 2012, 12:00:00

Is it possible to have optional parameters in a Backbone.js route?



e.g this:



routes:
search/[:query]: searchIndex


instead of:



routes:
search/: searchIndex
search/:query: searchIndex

More From » backbone.js

 Answers
44

As of Backbone 0.9.9, you can add optional paramaters with parentheses.



For example in your routes object you can define an optional route part like this:



routes: {
organize(/:action): displayOrganize
}


Now the url path will match /#organize and routes like /#organize/create.



Keep in mind that if you need routes like /#organize/ (with a trailing slash) to be recognized, you can do:



routes: {
organize(/)(:action): displayOrganize
}

[#87007] Tuesday, March 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mattieparisp

Total Points: 612
Total Questions: 109
Total Answers: 104

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;