2013-07-10 62 views
0

Durandal有没有办法获取当前模块的路径?我在SPA内部构建了一个仪表板,并且想要像Durandal对“FolderWidgetName”所做的那样组织我的小部件,并且该文件夹将包含一个controller.js和view.html文件。我尝试在我的controller.js文件中使用getView()方法,但无法使其在当前文件夹中查看视图。Durandal,获取当前模块的路径

getView(){ 
    return "view"; // looks in the "App" folder 
    return "./view"; // looks in the "App/durandal" folder 
    return "/view"; // looks in the root of the website 
    return "dashboard/widgets/htmlviewer/view" //don't want to hard code the path 
} 
  • 我不想硬编码在控制器内的路径
  • 我不想覆盖viewlocator,因为该应用程序还充当常规迪朗达尔温泉其余使用标准约定。

回答

1

你可以使用define(['module'], function(module) { ...为了得到保持当前的模块。 getView()将允许您设置特定的视图,或者像下面的示例一样在多个视图之间动态切换。

define(['module'], function(module) { 

    var roles = ['default', 'role1', 'role2']; 
    var role = ko.observable('default'); 
    var modulePath = module.id.substr(0, module.id.lastIndexOf('/') +1); 

    var getView = ko.computed(function(){ 
     var roleViewMap = { 
      'default': modulePath + 'index.html', 
      role1: modulePath + 'role1.html', 
      role2: modulePath + 'role2.html' 
     }; 

     this.role = (role() || 'default'); 

     return roleViewMap[this.role]; 
    }); 


    return { 
    showCodeUrl: true, 
    roles: roles, 
    role: role, 
    getView: getView, 
    propertyOne: 'This is a databound property from the root context.', 
    propertyTwo: 'This property demonstrates that binding contexts flow through composed views.', 
    moduleJSON: ko.toJSON(module) 
    }; 


}); 

这里是一个活生生的例子http://dfiddle.github.io/dFiddle-1.2/#/view-composition/getView

0

添加激活功能,以您的视图模型如下:

define([], 
    function() { 
     var vm = { 
      //#region Initialization 
      activate: activate, 
      //#endregion 
     }; 

    return vm; 

    //#region Internal methods 
    function activate(context) { 
     var moduleId = context.routeInfo.moduleId; 
     var hash = context.routeInfo.hash; 
    } 
    //#endregion 
}); 
+0

任何想法如何让生命周期事件使用撰写结合何时启用?我之前曾经遇到过这个问题,并且在Google工作组中提出了一个问题https://groups.google.com/forum/#!topic/durandaljs/PhNrL6skqtU但从未得到答案 – Tyler

+0

当您激活一条新路线只要您将其暴露给路由器,就会自动在先前的视图模型上调用停用。这似乎是你的问题在谷歌组。 –

+0

克里斯还需要从您的激活函数返回一个回调,这在示例中并不清楚。 –

0

您可以设置视图简单地绑定到router.activeRoute.name或.URL和应该做你在找什么。如果您在加载时试图回写设置viewmodels属性,则可以像下面这样做。

如果您使用揭示模块,您需要定义函数并创建模块定义列表并将其返回。例如:

define(['durandal/plugins/router', 'view models/setup'], 
    function(router, setup) { 

var myObservable = ko.observable(); 

function activate() { 
    setup.currentViewName = router.activeRoute.name; 
    return refreshData(); 
} 

var refreshData = function() { 
    myDataService.getSomeData(myObservable); 
}; 

var viewModel = { 
    activate: activate, 
    deactivate: deactivate, 
    canDeactivate: canDeactivate 
}; 

return viewModel; 
}); 

您也可以显示文字,观测,甚至直接的功能,同时露出他们 -

title: ko.observable(true), 
    desc: "hey!", 
    canDeactivate: function() { if (title) ? true : false, 

退房更多信息Durandal的路由器页面上什么是可用的。另外,Durandal 2.0正在切换路由器。

http://durandaljs.com/documentation/Router/

+0

该应用程序的主要部分使用路由和生命周期事件正常工作。如果我有一个问题基本上与子视图和使用组合绑定 – Tyler