Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query parameters in Durandal router

Tags:

durandal

I have a route that has query parameters. When activating the view, the query parameters are gathered and passed to a server side request. I don't know what the parameters are. I simply pass them to the server for processing.

In my viewmodel activate method, the context parameter is an object that contains the query parameters, plus two extra items: routeInfo and router. Durandal adds these to the query parameters. First, I don't want those passed on to the server side. Second, if my query parameters also contains those two names, then they get overwritten.

I've looked through the router code and find where they are added to my params (in dequeueRoute), but find no way to prevent the addition, nor a way to get just my query parameters without them.

Does anyone know how I can get the query parameters without the routeinfo and router? And out of curiosity, why are they added in the first place instead of a separate argument?

like image 371
arch-imp Avatar asked Mar 25 '13 22:03

arch-imp


1 Answers

Your right. I went ahead and tested what your saying and it seems that all the querystring parameters are being written to the context parameter.

so if you have a route like this:

http://www.somesite.com/#/services/?routeInfo=value1&router=value2&param1=value3

inside your activate method:

define(function() {
   return {
     activate: function(context) {
        console.log(context);
     }
   };
});

Then the context will be a json object of:

{ 
   routeInfo: {...}, // info object of moduleId and such
   router: {...}, // reference to the router.js pluggin
   param1: 'value3',
   splat: ['?routeInfo=value1&router=value2&param1=value3']
}

So, the querystring parameters are being written to the context object but if your querystring parameters are of routeInfo, router, or splat then bad things will happen. Especially if its splat!

So, I guess just dont use those parameters..

Also, as you can see your querystring parameters are passed to the splat object. You can easily parse the querystring by doing:

function parsePlease(queryString) {
   var obj = {};
   var splitV = queryString.split('&');
   for (var i = 0, max = splitV.length; i< max; i++) {
      obj[splitV[i].split('=')[0]] = splitV[i].split('=')[1];
   }
   return obj;
}

Something like that. You might want to improve on that.. i just whipped it up.

Also, you can also get your query string params from the window.location object. From the window.location.search object it will contain everything after the ? deliminator.

Does this answer your questions?

like image 62
Evan Larsen Avatar answered Sep 25 '22 15:09

Evan Larsen