Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing url params with Backbone routers - regex examples

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!

like image 292
unjust Avatar asked Dec 08 '11 16:12

unjust


2 Answers

I had the same problem. Here is a backbone plugin that does the job: https://github.com/jhudson8/backbone-query-parameters

Cheers!

like image 149
PreslavLe Avatar answered Nov 18 '22 04:11

PreslavLe


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....
like image 43
Thomas Davis Avatar answered Nov 18 '22 02:11

Thomas Davis