Eric Panorel 在这里有一篇关于如何使用 John Papa 的 Visual Studio SPA 模板执行此操作的精彩文章:http: //ericpanorel.net/blog/hot-towel-spa-master-detail-scenario。
简而言之,您可以利用 Durandal 中的路由,它会是这样的:
master.js
将公开一组项目:
define(function() {
var self = this;
this.items = ko.observableArray([]);
var activate = function() {
// ... get your items
self.items([ { id: 0, name: 'Item 1' }, { id: 1, name: 'Item 2' }]);
}
return {
// some properties and ...
activate: activate
}
});
master.html
将绑定它们并链接到详细信息视图:
<section data-bind="foreach: items">
<a data-bind="text: name, attr: { href: '#/item/' + id() }"></a>
</section>
现在,剩下的就是使用路由数据参数在详细视图模型中提取 ID detail.js
,:
define(function() {
var self = this;
this.name = ko.observable();
var activate = function(routeData) {
var itemId = routeData.id;
// ... get your item details
// var details = ...
self.name(details.name);
}
return {
// some properties and ...
activate: activate
}
});
最后,您需要在您的main.js
:
define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'durandal/plugins/router'],
function(app, viewLocator, system, router) {
//>>excludeStart("build", true);
system.debug(true);
//>>excludeEnd("build");
app.title = 'Durandal app';
app.start().then(function() {
toastr.options.positionClass = 'toast-bottom-right';
toastr.options.backgroundpositionClass = 'toast-bottom-right';
//Replace 'viewmodels' in the moduleId with 'views' to locate the view.
//Look for partial views in a 'views' folder in the root.
viewLocator.useConvention();
//configure routing
router.useConvention();
router.mapRoute('item/:id', 'viewmodels/item', 'Item', false);
app.adaptToDevice();
//Show the app by setting the root view model for our application with a transition.
app.setRoot('viewmodels/shell', 'entrance');
});
});
你去吧!