Search code examples
durandalsingle-page-applicationdurandal-navigation

Single Page App (SPA) with Durandaljs, Not Able to Load Default Route


I am trying to write a NopCommerce plugin. I have put in my app related files inside plugin's root directory in app named directory. In my shell.js viewmodel I define routes as follows:

define(['durandal/system', 'durandal/plugins/router', 'service/logger','config'],
    function(system, router, logger,config) {

        var shell = {
            activate: activate,
            router: router
        };

        function activate() {
            logger.log('MyProducts Started', null, system.getModuleId(shell), true);
            router.map([
                { route: '', moduleId: 'viewmodels/myProducts', title: 'My Products', nav: true },
                { route: 'searchProducts', moduleId: 'viewmodels/searchProduct', title: 'Search Products', nav: true },
                { route: 'addProducts', moduleId: 'viewmodels/addProduct', title: 'Add Product', nav: true }
            ]).buildNavigationModel();
            return router.activate();
        }

        return shell;
    }
);

Per conventions it should go to first route defined by module: viewmodels/myProducts but I am getting following error:

[viewmodels/shell] MyProducts Started system.js:75
[main] No router found Object
Navigation Complete undefined Object
Uncaught TypeError: Cannot read property 'router' of undefined 

I am banging my head with the well. It is not going to default route (with route:'').


Solution

  • Resolved :)

    After much headache, I found out that this is causing the issue:

    define([..., 'durandal/plugins/router',...]
    

    When I remove this and it was resolved.

    EDIT:- Following is define function inside my main.js file.

    define(['durandal/system', 'durandal/app', 'durandal/viewLocator'
                                                                , 'service/logger'],
    function (system, app, viewLocator, logger) {
        system.debug(true);
    
        app.configurePlugins({
            router: true,
            dialog: true,
            widget: true
        });
    
        app.start().then(function () {
            viewLocator.useConvention();
            app.setRoot('viewmodels/shell', 'entrance');
    
            //router.mapUnknownRoutes(function (instruction) {
            //    logger.logError('No router found', instruction, 'main', true);
            //});
        });
    });