I'm using Backbone.js and I have a url to my collection followed by a param string - the params can occur in any order, the number of params can vary.
mywebsite.com/?orderBy=recent&author=Smith
mywebsite.com/?author=Smith&type=Horror,Romance
So in a way the /:param isn't ideal unless I define an order that they have to be in in the url:
/:orderBy/:author/:type...
and allow some of them to be null somehow.
What's the best way to set this up? Would a router with regex answer my problems? If so, I can't find examples of a router using regex to pass multiple arguments to a routing function in Backbone.
Thanks!
I had the same problem. Here is a backbone plugin that does the job: https://github.com/jhudson8/backbone-query-parameters
Cheers!
I don't believe your url parameters should map query strings much like how restFul interfaces don't map the query strings.
/cars?filterby=12 would be more correct then some sort of /cars/filterby/12
I understand rest isn't an applications routes but they still have the same caveats.
jQuery contains a $.param
function for easily dealing with querystrings and jQuery BBQ contains a $.deparam
util which can parse querystrings
Keep your routes simply
mywebsite.com/#/authors?orderBy=recent....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With