RobVious
RobVious

Reputation: 12915

Durandal Routing - routing to a resource with a particular id?

I'm trying to configure my routing so that navigating to #/my displays the contents for #/folder/62 (or some id stored in a variable) - and navigating to #/public displays the contents for #/folder/1 (same concept).

Additionally, I'd like the application to navigate to one of these routes upon loading, depending on whether or not the user is authenticated. The authentication stuff is done, but once the above routes have been configured, I'd like to activate on one of them.

Here's what I have:

        activate: function () {
        router.useConvention();
        router.handleInvalidRoute = function (route, params) {
            //debugger;
            logger.logError("Invalid route", route, null, true);
        };

        router.map([                
            { url: 'home', moduleId: 'viewmodels/home', name: 'Home', visible: false },               
            { url: 'my', moduleId: 'viewmodels/folder', name: 'My Content', visible: false }, // Should display contents of /folder/2
            { url: 'public', moduleId: 'viewmodels/folder', name: 'Public Content', visible: false }, // Should display contents of /folder/3
            { url: 'set/:id', moduleId: 'viewmodels/set', name: 'Set', visible: false },
            { url: 'folder/:id', moduleId: 'viewmodels/folder', name: 'Folder', visible: false }
        ]);

        if (auth.isAuthenticated)
            return router.activate('my'); // should show details page of a particular folder
        else {
            return router.activate('public'); // should show details page of a particular folder
        }
    }

Upvotes: 1

Views: 599

Answers (1)

RainerAtSpirit
RainerAtSpirit

Reputation: 3723

app.setRoot as described in https://groups.google.com/forum/#!searchin/durandaljs/app.setRoot/durandaljs/t1hrLfOh1oM/RtCekmY0bDAJ could be used to show different views for authenticated/non-authenticated users.

If only authenticated users should be allowed to see content of "folder/specialID" in the example then you might consider not to expose these via router. Use standard compose functionality in #my to load the specialID view/view model instead.

Upvotes: 2

Related Questions