1

I have two modules that I want to connect by navigating from one to the other. Since there are going several parameters to be passed, I want to use query string. Is there a way to use the built-in router to navigate to a module and have my object arguments being passed like

var params = {
  param1: "parameter",
  param2: "anotherparameter"
}
router.navigate("theothermodule", params);

or do I have to manually parametrize the object into the url string and pass it to router.navigate like

router.navigate("theothermodule?" + $.param(params));
patryk
  • 651
  • 12
  • 30

2 Answers2

1

Another solution would be :

var params = {
  param1: "parameter",
  param2: "anotherparameter"
}
//notes : this object can contain arrays like knockout observable()
//or observableArray();

1.- create the object (params) in shell.js and map it to your route definition like bellow :

 { route: 'routestr/:id', moduleId: 'viewmodels/adir/mymodul', title: 'je suis', 
hash: '#routestr', nav:3, routobj: params }

2.- you could update, even add new members to your object from anywhere before calling this route, by hacking the object from the route.

var myrouteconfig = router.activeInstruction().config;
myrouteconfig.param2 = newvalue;
myrouteconfig.newdlyParam = value;
router.navigate("#routestr/" + id);

3.- Then, in the receipt module :

var activate = function (id) {
      //var myid = parseInt(id); //noelem rubriq                    
      var myrouteconfig = router.activeInstruction().config;
      var mycallobject = myrouteconfig.params; //your object params
      var valparam2 = mycallobject.param2; // your param2 last value
      ......
}
M.Ouedraogo
  • 195
  • 1
  • 6
  • Does this put the serialized `params` object into the url as query string? As I can see, you use the id route parameter only as a part of the url and provide the module args 'under the hood'. What I want to achieve is to be able to pass any object as an argument and receive it in the target module while *most important* having all its properties in the url. The url is then simply the bridge for all the args. What I have done is I wrapped the Durandal's router and overrode the navigate method to make it receive additional third argument. Simple but works as expected. – patryk Oct 08 '13 at 19:52
-1

You have to manually parametrize the object into the url string and map in router.activate (http://durandaljs.com/documentation/Using-The-Router/).

However you can share data with Events(http://durandaljs.com/documentation/Leveraging-Publish-Subscribe/) or Activation Data property in Composition binding(http://durandaljs.com/documentation/Using-Composition/).

edotassi
  • 476
  • 2
  • 6
  • 19
  • This is the case where the url is desired so it can be easily shared or bookmarked. Still even though it is not that hard to make it myself, I am really surprised that the router does not come with such simple and intuitive functionality. Anyway thank you for making things clear. – patryk Oct 04 '13 at 16:05
  • you're right, the documentation has been moved here: http://durandaljs.com/documentation/api.html – edotassi Apr 14 '14 at 08:16