路由器上有一个navigateTo
功能应该可以满足您的需求。
根据您的描述,我假设详细信息和订单在不同的页面上。
在 Durandal 中,通常会有页面(视图模型)名称后跟参数。所以对于这个答案,我将假设客户详细信息和客户订单的页面名称,每个都有一个 id 参数。
在这种情况下,您需要一个路由设置来处理如下参数:
router.mapNav("customer-details/:id");
router.mapNav("customer-details");
router.mapNav("customer-orders/:id");
使用此映射,您应该能够导航到带有或不带有 id 的客户详细信息页面。
据我所知,Durandal 1.x 并不真正支持从激活函数中重新导航。因此,您可能会考虑:
- 允许没有指定 id 的路由,但不强制它重新导航到特定于 id 的路由。
- 在导航到详细信息页面之前获取默认客户 ID。
假设选项 1:
在 activate 函数中,您可以检查是否传递了 id:
function activate(context){
//load the customer dropdown source
...
if( !context.hasOwnProperty("id") ){
//id was not supplied, so set the current customer id
// to the first one in the customer dropdown source
...
}
}
至于导航栏,设置导航链接可能是最简单的,因此它们是通用的。您可以将链接的单击绑定到视图模型中的函数,并从那里以编程方式导航。这样你就可以应用任何必要的逻辑。
看法
//in the view
<select data-bind='options: customers, value: selectedCustomer'></select>
<a data-bind='click: showDetails'>Details</a>
| <a data-bind='click: showOrders'>Orders</a>
查看模型
//in the view model
// (these members need to be expose publically so knockout can access them)
var customers = ko.observableArray();
var selectedCustomer = ko.observable();
function showDetails(){
if( this.selectedCustomer() ){
var id = this.selectedCustomer().id;
router.navigateTo("#/customer-details/" + id);
}
};
function showOrders(){
if( this.selectedCustomer() ){
var id = this.selectedCustomer().id;
router.navigateTo("#/customer-orders/" + id);
}
};
希望这会有所帮助 - 它不是 100%,但无论如何应该给你一些想法。