使用路由器导航并将对象作为查询字符串传递

时间:2013-10-04 10:55:05

标签: javascript router durandal durandal-navigation

我想通过从一个导航到另一个来连接两个模块。由于要传递几个参数,我想使用查询字符串。有没有办法使用内置路由器导航到模块并让我的对象参数像

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

或者我是否必须手动将对象参数化为url字符串并将其传递给router.navigate,如

router.navigate("theothermodule?" + $.param(params));

2 个答案:

答案 0 :(得分:1)

另一种解决方案是:

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

1.-在shell.js中创建对象(params)并将其映射到您的路由定义中     吼叫:

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

2.-您可以更新,甚至可以在调用此路线之前从任何地方向您的对象添加新成员,方法是从路线中删除对象。

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

3.-然后,在收据模块中:

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
      ......
}

答案 1 :(得分:-1)

您必须手动将对象参数化为url字符串并映射到router.activate(http://durandaljs.com/documentation/Using-The-Router/)。

但是,您可以在合成绑定(http://durandaljs.com/documentation/Leveraging-Publish-Subscribe/)中与事件(http://durandaljs.com/documentation/Using-Composition/)或激活数据属性共享数据。