Projekt "próbki" dostarczany z Durandal 2.0 jest przykładem tego, jak osiągnąć to, czego pragniesz, bez konieczności dostosowywania routera. Poniższy przykład pochodzi z tego projektu (a także pokazuje, jak używać routera "podrzędnego"). Zwróć uwagę, w jaki sposób parametry moduleId w wywołaniu funkcji "makeRelative" i konfiguracja trasy łączą się, aby uzyskać dowolną wybraną strukturę folderów.
APP/ko/index.js
define(['plugins/router', 'knockout'], function(router, ko) {
var childRouter = router.createChildRouter()
.makeRelative({
moduleId:'ko',
fromParent:true
}).map([
{ route: '', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro' },
{ route: 'helloWorld', moduleId: 'helloWorld/index', title: 'Hello World', type: 'intro', nav: 5},
{ route: 'clickCounter', moduleId: 'clickCounter/index', title: 'Click Counter', type: 'intro', nav: 4},
{ route: 'simpleList', moduleId: 'simpleList/index', title: 'Simple List', type: 'intro', nav: 3 },
{ route: 'betterList', moduleId: 'betterList/index', title: 'Better List', type: 'intro', nav: 2},
{ route: 'controlTypes', moduleId: 'controlTypes/index', title: 'Control Types', type: 'intro', nav: 1 },
{ route: 'collections', moduleId: 'collections/index', title: 'Collection', type: 'intro' , nav: 99 },
{ route: 'pagedGrid', moduleId: 'pagedGrid/index', title: 'Paged Grid', type: 'intro', nav: 98 },
{ route: 'animatedTrans', moduleId: 'animatedTrans/index', title: 'Animated Transition', type: 'intro', nav: true },
{ route: 'contactsEditor', moduleId: 'contactsEditor/index', title: 'Contacts Editor', type: 'detailed', nav: true },
{ route: 'gridEditor', moduleId: 'gridEditor/index', title: 'Grid Editor', type: 'detailed', nav: true },
{ route: 'shoppingCart', moduleId: 'shoppingCart/index', title: 'Shopping Cart', type: 'detailed', nav: true },
{ route: 'twitterClient', moduleId: 'twitterClient/index', title: 'Twitter Client', type: 'detailed', nav: 1}
])
.buildNavigationModel();
return {
router: childRouter,
introSamples: ko.computed(function() {
return ko.utils.arrayFilter(childRouter.navigationModel(), function(route) {
return route.type == 'intro';
});
}),
detailedSamples: ko.computed(function() {
return ko.utils.arrayFilter(childRouter.navigationModel(), function(route) {
return route.type == 'detailed';
});
})
};
});
Widziałem twoje inne odpowiedzi na posty dotyczące nadpisania tej funkcji. Gdzie jest najlepsze miejsce, aby to zrobić? W shellu.js przed powrotem? Czy dlatego router jest parametrem dla modułu shell.js, więc możesz zastąpić jego funkcje w tym miejscu? Czy jest jakieś inne miejsce, które byłoby lepsze, takie jak moduł main.js? Dzięki! – Jammerms
Gdziekolwiek to zrobisz, umieściłbym wszystkie Twoje początkowe konfiguracje durand w 1 miejscu. W ten sposób łatwo będzie ją znaleźć i utrzymać. –
Czy istnieje najlepsza praktyka, aby je umieścić? Czy umieszczasz swoje dostosowania w pliku shell.js lub main.js, czy gdzieś indziej? Jeszcze raz dziękuję. – Jammerms