這可能是代碼結構的問題,而不是關於框架的問題。父路由器激活功能每次將子路由導航到?
基本上我想知道是否應該在父路由器上的激活函數每次導航到其子路由時調用?
我的目標: 有一個返回子路由器的模塊,這是父路由器。如果用戶正在添加新的東西,則該父路由器負責創建對象,或者如果用戶選擇了已存在的東西,則從服務器檢索數據。子路由模塊只需獲取通過父路由器模塊創建或檢索的對象,並提供表單輸入來操作該對象(通過ko數據綁定)。我遇到的問題是父路由器的激活函數在每次導航到子路由時都會被調用,這是創建/檢索邏輯所在的位置,並且會再次檢索對象,導致任何數據操作被覆蓋。
下面是一些代碼,希望能更好地說明:
// ParentRouter
define([...], function(...) {
var selectedObj = ko.observable(null),
configRouter = router.createChildRouter()
.makeRelative({
moduleId: 'viewmodels',
fromParent: true,
dynamicHash: ':id'
}).map([
{
route: ['Settings 1', ''],
moduleId: 'SettingsOne',
nav: true
},
{
route: 'Settings 2',
moduleId: 'SettingsTwo',
nav: true
},
{
route: 'Settings 3',
moduleId: 'SettingsThree',
nav: true
}
]).buildNavigationModel();
function getSelectedObj() {
return selectedObj;
}
return {
router: configRouter,
obj: selectedObj, // privileged property which is accessed by other modules via the getSelectedObj method, this prop gets set in the activate function depending on the param
getSelectedObj: getSelectedObj, // method which returns the current selected obj
activate: function (objId) {
if (objId == 'New') {
// create a new object with default props
} else {
// retrieve the data for the selected obj from API via the 'objId' param
}
}
}
});
// SettingsOne module - child route module
define(['viewmodels/parentRouter'], function(parentRouter) {
// this module simply gets a property from the parent routers module and binds it to form inputs in its View (which update automatically) so the user can edit information on that obj
var obj = parentRouter.getSelectedObj(); // get the current selected obj via parent routers module method
return {
obj: obj
}
});
Below is the shell module of my app, showing the main application router.
// Shell.js
define([
'plugins/router'
], function (router) {
var routes = [
{
route: '',
moduleId: 'viewmodels/home'
},
{
route: ':id*configs',
moduleId: 'viewmodels/parentRouter'
}
];
return {
router: router,
activate: function() {
return router.map(routes)
.mapUnknownRoutes('viewmodels/notfound', 'not-found')
.activate();
}
}
});
非常感謝。這基本上是我目前正在做的。只是不覺得這是由杜蘭德設計? –