RobVious
RobVious

Reputation: 12915

HandleInvalidRoute not working with Durandal

Here's my shell.js vm:

var vm = {
    router: router,
    auth: auth,
    viewAttached: function () {

    },
    activate: function () {
        router.useConvention();
        router.handleInvalidRoute = function (route, params) {
            debugger;
            toastr.info('No Route Found: ' + route);
        };
        router.map([
            { url: 'error', moduleId: 'viewmodels/error', name: 'Error', visible: false }           
        ]);

        router.mapAuto();

        if (auth.isAuthenticated)
            //return router.activate('folder/2');
            return router.activate('home');
        else {
            return router.activate('home');
        }
    }
};


return vm;
});

When I navigate to an invalid route (/folders, for example), the debugger in my handleInvalidRoute block isn't hit, and I get a scripterror from require.js:

GET http://appname.com/App/viewmodels/folders.js 404 (Not Found) require.js:33 Uncaught Error: Script error http://requirejs.org/docs/errors.html#scripterror require.js:8 J require.js:8 j.onScriptError

That's all I have to work with. Any idea what's going on?

Upvotes: 0

Views: 594

Answers (1)

RainerAtSpirit
RainerAtSpirit

Reputation: 3723

This has been answered by @EisenbergEffect in the Durandal newsgroup https://groups.google.com/forum/#!topic/durandaljs/eZrIcgn3aU8.

It is because you called mapAuto which always attempts to map urls to modules, whether or not they actually exist. Effectively, handleInvalidRoute will never be called.

Upvotes: 1

Related Questions