0

I'm trying to extend an existing Durandal router plugin instance already created with the help of RequireJS.

The method map() should be overriden to add extra mapping parameters.

How should I access the original method from the modified one?

index.js

define([ 'durandal/app', 'app/childRouter'], function(
        app, childRouter) {
    childRouter.map([ {
        route : 'details/:id',
        moduleId : 'details/index',
    }, {
        route : 'details/tabs/base',
        moduleId : 'details/tabs/base',
    } ]);
    childRouter.buildNavigationModel();

    return {
        router : childRouter
    };
});

childRouter.js

define([ 'durandal/app', 'plugins/router'], function(
        app, router) {

    var childRouter = router.createChildRouter();
    childRouter._map = childRouter.map;
    childRouter.map = function(data) {
        data.unshift({
            route : [ '', 'grid' ],
            moduleId : 'grid/index',
        });
        childRouter._map(data);//CALLS THE OVERRIDEN METHOD AGAIN INSTEAD OF ORIGINAL
    };
    return childRouter;
});
Peter G.
  • 7,816
  • 20
  • 80
  • 154

1 Answers1

1

If you want to still call the original "map" function, you'll need to store it before you overwrite it. It's a bit "hacky" to replace functions in this way, but if you REALLY had to do it, this will work:

var childRouter = router.createChildRouter();
childRouter.originalMap = childRouter.map;  // Save original
    childRouter.map = function(data) {
        data.unshift({
            route : [ '', 'grid' ],
            moduleId : 'grid/index',
        });
        childRouter.originalMap(data);    // Call original
    };
jestermax
  • 21
  • 1
  • 2
  • Tried it, but instead of calling the original `map()` it calls the overriden one again. I don't understand what's going on? – Peter G. Feb 12 '16 at 09:32
  • I need a small clarification: are you intending on calling the original map function inside of the overridden one? or do you want it callable just in general? – jestermax Feb 12 '16 at 13:14
  • Yes exactly, it should be callable inside the overriden one. I found it works outside Durandal (for example when you create JSFiddle), but not inside. I tried debugging the sequence of in which the functions get called and found nothing unusual. I wonder why the function calls itself again instead of the stored original. – Peter G. Feb 12 '16 at 16:10