Will Backbone.Router.navigate
set test
to true
:
var test = false;
var Router = Backbone.Router.extend({
routes: {
'posts': 'showPosts'
},
showPosts: function () {
test = true;
}
});
router = new Router();
Backbone.history.start();
router.navigate('posts?foo=3', {trigger: true});
assert.ok(test);
Eg, will posts?foo=3
fragment will match the posts
route by default, or do I have to set another route for that, for example: posts?*querystring
?
Thank you
PS: I know there exist the backbone-query-parameters but I want to know just for backbone.
Here's another take, still using lodash (underscore). Removed the _.map, added a bit of verbosity to the variables, and stripped out the starting '?' if present:
Just to complement the previous answers, instead of defining two routes that have the same callback, like:
You could have only one route to keep the code cleaner:
You need to add another route with that expecting parameter :
update
You could define the general route to return all the query string and then parse it in the handler :
update 2
Here's a live demo to see the code in action.
RFC 3986 "syntax for URIs" states that query parameters shold come before hash fragment.
I have this issue handling a redirect I am getting from the server i.e. "http://foo.com/main.html?error=errormessage#site". I would like to route on the query but can't see a way to write the backbone route expression to handle this url. For now I just route on the hash and check for a query by parsing location.search.
Backbone docs:
If you still want to keep the functionality without the matching you can define two routes