menacingly
menacingly

Reputation: 738

Marionette Router Query String Parameters In URL Fragment Routes

I'm working on a project that requires that most UI state is reproducible via URL. In a traditional (server-side) app, I could use both URL parameters like:

/resources/:id

and unordered optional query string parameters, like:

/resources/:id?page=5&sort=date

Is there an idiomatic way to achieve this with Backbone/Marionette routing? I don't want to have to configure routes for every possible combination of parameters.

The fact that I don't see this addressed much makes me think I may be barking up the wrong tree, approach-wise, but I do think being able to represent as much UI state as possible in the URL is pretty important to a lot of projects.

Upvotes: 1

Views: 1246

Answers (2)

Tiago Romero Garcia
Tiago Romero Garcia

Reputation: 1078

URL parameters are not really enforced by Backbone/Marionette. One possible reason is that URL parameters are not SEO friendly.

Instead, you can configure optional URL fragments which will work pretty much like URL parameters, this way:

/resources/:id(/page/:page)(/sort/:sort)

If you do this way, the only gotcha here is that this sequence of "parameters" need to be ordered.

HOWEVER if you need it to be unordered, you can simply use Regular Expressions with router.route() method inside your initialize, as explained in Router#route

Upvotes: 0

menacingly
menacingly

Reputation: 738

It looks like the best option is the now-orphaned backbone-query-parameters project.

It supports routes exactly in the form I'm looking for:

#resources/:id?flag=true

Upvotes: 1

Related Questions