2

I try to find title for the current page (navigation)... the only way that work for me was: that is a little ugly.

<!-- ko foreach: router.navigationModel -->
    <h1 data-bind="text: title, visible: isActive"></h1>
<!-- /ko -->

A better way will be this one... but I don't know the current index page

<!--<h1 data-bind="text: router.navigationModel()[____INDEX____].title"></h1>-->

Any suggestion to addressing this problem in a better way???

Jaider
  • 14,268
  • 5
  • 75
  • 82
  • 1
    I like the answer provided by @gerrod in one of his comments below using `router.activeInstruction().config.title` – Jaider Nov 16 '13 at 16:48

1 Answers1

1

Durandal's router does not store the index of the current route, it only provides the isActive property to check whether a a given route is active or not. So the only options is search for the isActive route.

Instead of using the foreach in your view you can create a computed in your shell.js which encapsultes this route finding logic:

currentTitle: ko.computed(function () {
    var activeRoute = ko.utils.arrayFirst(router.navigationModel(), 
         function (item) {
            return item.isActive();
    });
    if (activeRoute)
        return activeRoute.title;
})

And now in your shell.html you can just write:

<h1 data-bind="text: currentTitle"></h1>

Alternatively as gerrod pointed out in a comment you can use activeInstruction property of the router from where you can get the current config and from there the title:

<h1 data-bind="text: router.activeInstruction().config.title"></h1>
Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    You could alternatively check `router.activeInstruction().config` - this is the active route. – gerrod Nov 14 '13 at 03:26
  • 1
    @gerrod thanks for the info about the `activeInstruction` I've included it in my answer. – nemesv Nov 14 '13 at 06:16