i trying to create child routes in durandal 2 from my database starterkit without success.
Although I succeeded in rendering it with static contents as shown below:
define(['plugins/router', 'knockout'], function (router, ko) {
var childRouter = router.createChildRouter()
.makeRelative({
moduleId: 'sectionGroups',
fromParent: true
}).map([
{ route: ['', 'child1'], moduleId: 'pages/child1', title: 'child1', nav: true },
{ route: 'child2: moduleId: 'pages/child2', title: 'child2', nav: true },
{ route: 'child3', moduleId: 'pages/child3', title: 'child3', nav: true }
]).buildNavigationModel();
return {
router: childRouter
};
});
However I've been unable to dynamically output the the menus from the database. Am i doing somethig wrong? Please see my code below
define(['plugins/router', 'knockout','services/oasisManager', 'services/logger'], function (router, ko, om, logger) {
var pageRoutes = getPageRoutes();
var childRouter = router.createChildRouter()
.makeRelative({
moduleId: 'sectionGroups',
fromParent: true
})
.map(pageRoutes)
.buildNavigationModel();
function getPageRoutes() {
var pageRoutes = [];
om.getOasis(134).done(function() {
ko.utils.arrayForEach(om.activeOasis.PageRoutes, function(item) {
var nav = true;
if (item.Route == '') {
nav = false;
}
pageRoutes.push({
route: item.Route,
moduleId: item.ModuleId,
title: item.Title,
nav: nav
});
});
});
return pageRoutes;
}
return {
router: childRouter
};
});
Html looks pretty much the same. Please help
<div class="container-fluid knockout-samples">
<div class="row-fluid">
<div class="span2 well">
<ul class="nav nav-list">
<li class="nav-header">Basic Examples</li>
<!--ko foreach: router.navigationModel-->
<li data-bind="css: { active: isActive }">
<a data-bind="attr: { href: hash }, text: title"></a>
</li>
<!--/ko-->
</ul>
</div>
<div class="span10">
<!--ko router: { transition:'entrance', cacheViews:true }--><!--/ko-->
</div>
</div>
I think what is happening in your code is that getPageRoutes
is returning a blank array. it should be returning a promise for a list of routes. you probably need something like this.
function getPageRoutes() {
return om.getOasis(134).done(function(routes) {
var pageRoutes = [];
ko.utils.arrayForEach(routes, function(item) {
var nav = true;
if (item.Route == '') {
nav = false;
}
pageRoutes.push({
route: item.Route,
moduleId: item.ModuleId,
title: item.Title,
nav: nav
});
});
return pageRoutes;
});
}
I am making the assumtion that om.getOasis(134)
will return a promise of for a list of routes.
Not sure if this will help but this is what I have in my durandal app to do something similar to what you want to achieve. I use the Q promises library to handle the async calls to the webapi.
define(['plugins/http', 'services/logger'], function (http, logger) {
"use strict";
var getRoutes = function () {
return http.get("routes/availableRoutes").then(function (data) {
logger.log("Routes Loaded", data, 'services/routes', false);
return data;
});
};
var routes = {
getRoutes: getRoutes
};
return routes;
});
The calling code then becomes something like the following.
return routes().then(function (availableRoutes) {
return router.makeRelative({
moduleId: 'sectionGroups',
fromParent: true
})
.map(availableRoutes)
.buildNavigationModel()
.activate();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.