如何配置Durandal.js以在同一文件夹中包含多个带有视图和视图模型的区域?

Jam*_*rms 6 single-page-application durandal

我正在使用一个SPA,它将有许多不同的区域,每个区域都有几页.我的老板真的不想要几十个视图和viewmodels文件夹,每个文件夹中包含几个项目.如何配置Durandal使用以下内容:

/App/Areas/Login/
                 login.html
                 login.js
/App/Areas/Here/
                 subpage1.html
                 subpage1.js
                 subpage2.html
                 subpage2.js
                 subpage3.html
                 subpage3.js
/App/Areas/There/
                 subpage1.html
                 subpage1.js
                 subpage2.html
                 subpage2.js
                 subpage3.html
                 subpage3.js
Run Code Online (Sandbox Code Playgroud)

我看过其他关于区域的类似问题,但不太清楚如何开始.谢谢.

Eva*_*sen 5

你可以拥有心中所需的任何文件夹结构.Durandal不会在您的应用程序上强加任何文件夹结构,但它具有完全可以覆盖的默认约定.

如果您使用Durandals 路由器,那么您将需要研究如何配置它以查找模块.有很多方法可以做到这一点,我更喜欢通过覆盖它来创建自己的约定router.autoConvertRouteToModuleId.

如果你没有使用路由器插件,那么你必须自己管理你的模块的uris,这是通过遵循requirejs的约定并沿着w/durandals 组合模块使用这个约定来完成的.

此外,您可以通过覆盖viewlocators约定来覆盖它如何找到要绑定到模块的视图.Durandal提供了一种非常简单的方法来构建小型应用程序,但是如果您需要构建更大的应用程序,那么建议您创建自己的约定.


Zac*_*ris 5

与Durandal 2.0一起提供的"样本"项目是如何在不必定制路由器的情况下完成所需的示例.下面的示例来自该项目(还显示了如何使用'子'路由器).请注意调用'makeRelative'和route config的moduleId参数如何组合以提供您想要的任何文件夹结构.

应用程序/ 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';
        });
    })
};
Run Code Online (Sandbox Code Playgroud)

});