You can achieve this by overriding the default behavior of the router process to set the header.
The title is always set after the navigation is completed, so the activation method of your view was previously called. Current implementation in Durandal 2.0:
router.updateDocumentTitle = function(instance, instruction) { if (instruction.config.title) { if (app.title) { document.title = instruction.config.title + " | " + app.title; } else { document.title = instruction.config.title; } } else if (app.title) { document.title = app.title; } };
This is called in the completeNavigation method in router.js .
In instance param, you have a ViewModel that you are activating, so a possible solution would be to override the updateDocumentTilte function in shell.js or main.js and use instance to get the values โโyou want, For example, you can do something like this ( make sure you have an instance of app and router ):
router.updateDocumentTitle = function (instance, instruction) { if (instance.setTitle) document.title = instance.setTitle(); else if (instruction.config.title) { if (app.title) { document.title = instruction.config.title + " | " + app.title; } else { document.title = instruction.config.title; } } else if (app.title) { document.title = app.title; } };
In this code, we check if the instance (current ViewModel) setTitle method, if it does, we get the header that calls the function. Then in our viewmodel we can have something like:
define(function () { var id; var vm = { activate: function (param) { id = param; return true; }, setTitle: function () { return 'My new Title ' + id;
If your viewmodel does not contain this method, it should fall to the current behavior.
source share