Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigate route with querystring

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.

like image 544
abernier Avatar asked Jul 26 '12 14:07

abernier


People also ask

How do you use a Querystring?

A Query String Collection is used to retrieve the variable values in the HTTP query string. If we want to transfer a large amount of data then we can't use the Request. QueryString. Query Strings are also generated by form submission or can be used by a user typing a query into the address bar of the browsers.

What is difference between navigate and navigateByUrl?

difference between the two navigateByUrl is similar to changing the location bar directly–we are providing the “whole” new URL. Whereas router. navigate creates a new URL by applying an array of passed-in commands, a patch, to the current URL.

What is Querystring in URL?

On the Internet, a querystring (also called an HTTP querystring) is part of the set of characters automatically input in the address bar of a dynamic Web site when a user makes a request for information according to certain criteria.


1 Answers

You need to add another route with that expecting parameter :

routes: {     'posts?foo=:foo' : 'showPosts',     'posts': 'showPosts' }, showPosts: function (foo) {     if(typeof foo != 'undefined'){        // foo parameters was passed     }     test = true; } 

update
You could define the general route to return all the query string and then parse it in the handler :

routes: {    'posts': 'showPosts',    'posts?*queryString' : 'showPosts' }, showPosts: function (queryString) {     var params = parseQueryString(queryString);     if(params.foo){         // foo parameters was passed     } }   ... // and the function that parses the query string can be something like :  function parseQueryString(queryString){     var params = {};     if(queryString){         _.each(             _.map(decodeURI(queryString).split(/&/g),function(el,i){                 var aux = el.split('='), o = {};                 if(aux.length >= 1){                     var val = undefined;                     if(aux.length == 2)                         val = aux[1];                     o[aux[0]] = val;                 }                 return o;             }),             function(o){                 _.extend(params,o);             }         );     }     return params; } 

update 2

Here's a live demo to see the code in action.

like image 53
gion_13 Avatar answered Oct 14 '22 07:10

gion_13