2013-07-10 219 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