router.guardRoute is not being called

2019-08-12 15:50发布

问题:

This is probably a simple one..

I am trying to override the routers guardRoute function and it seems that my version is not being called.

Code

app.start().then(function () {
    router.useConvention();
    viewLocator.useConvention();
    app.setRoot('viewmodels/shell', 'entrance');

    router.handleInvalidRoute = function (route, params) {
        logger.logError('No route found', route, 'main', true);
    };

    router.guardRoute = function (routeInfo, params, instance) {
        logger.logError('guardRoute called', routeInfo, 'main', true);
        return false;
    };
});

Edit 1 - Entire main.js file shown

require.config({
    paths: { "text": "durandal/amd/text" }
});

define(function (require) {
   var system = require('durandal/system'),
        app = require('durandal/app'),
        router = require('durandal/plugins/router'),
        viewLocator = require('durandal/viewLocator'),
        logger = require('services/logger');

    system.debug(true);

    app.title = "my app";

    app.start().then(function () {
        router.useConvention();
        viewLocator.useConvention();
        app.setRoot('viewmodels/shell', 'entrance');

        router.handleInvalidRoute = function (route, params) {
            logger.logError('No route found', route, 'main', true);
        };

        router.guardRoute = function (routeInfo, params, instance) {
            logger.logError('guardRoute called', routeInfo, 'main', true);
            //return false;
        };
    });

});

I do not get my log message and the router continues to process the request. What am I doing wrong?

回答1:

SO here is my answer ;) I have just checked the release form of Durandal 1.2.0 https://github.com/BlueSpire/Durandal/blob/master/Changes.txt And yes the guardRoute is added from this version.

You can check which versions you have in the packages.config file!

The hottowel template you own is probably version 1.1.1 of DurandalJS, this I can see in the NuGet Package Manager. If I want to download the HotTowel template right now it contains DurandalJS 1.1.1

You should update Durandal in the NuGet Manager and also the Durandal Router, then everything should work fine!

I have done this update to (but not from hottowel) and I clicked update ALL, DON'T click this, this will also update/ download Durandal Starterskit and your back at '0'.

Hoped it helps ;)

Greetings from the Netherlands, Leroy