DurandalJS Routing

What i have

An attempt to understand what is happening and how to control it. I have a β€œpublic” view for users who have not yet authenticated, and a β€œhome” view for authenticated users. Here is my route configuration:

app.start().then(function() { //Replace 'viewmodels' in the moduleId with 'views' to locate the view. //Look for partial views in a 'views' folder in the root. viewLocator.useConvention(); //configure routing router.useConvention(); router.mapRoute('home', 'viewmodels/home', 'Test App', true); router.mapRoute('public', 'viewmodels/public', 'Test App', true); router.mapRoute('set/:id', 'viewmodels/set', 'Set'); router.mapRoute('folder/:id', 'viewmodels/folder', 'Folder'); router.mapRoute('api', 'viewmodels/api', 'API Reference'); router.mapRoute('error', 'viewmodels/error', 'Error', false); app.adaptToDevice(); //Show the app by setting the root view model for our application with a transition. if (dataservice.isAuthenticated() === true) { app.setRoot('viewmodels/shell', 'entrance'); router.navigateTo('home'); } else { app.setRoot('viewmodels/public'); router.navigateTo('#/public'); } router.handleInvalidRoute = function (route, params) { logger.logError('No route found', route, 'main', true); router.navigateTo('#/error'); }; }); 

Problems

When I launch the application for the first time, I am not authenticated and receive an error message:

 Uncaught TypeError: Cannot call method 'lookupRoute' of undefined 

The appearance of the string 'router.navigateTo('#/public');' .

Then, when I try to press the login button, I get the same error:

 define(['durandal/app', 'durandal/plugins/router', 'services/dataservice'], function (app, router, dataservice) { var publicViewModel = function () { self.logIn = function () { app.setRoot('viewmodels/shell'); router.navigateTo('#/home'); }; 

But the content is loading correctly. When I go to a specific page by pressing, say in / folder / 2, and then change the url to / folders / 2 (invalid), I get a "route not found" in my log, as expected, but I come across a few other problems:

  • I do not get the error page or any errors (it seems to me that I need it for my handleInvalidRoute)
  • If I click on another, the URL will not change, and the new content will not be downloaded, again without errors.

I think I'm spoiling somehow, but I'm not sure how to do this. How can I fix the above problems?

Screen:

enter image description here

+4
source share
1 answer

I suspect you are calling navigateTo, where you might be too early for some reason. To test this theory, try moving this code.

  if (dataservice.isAuthenticated() === true) { app.setRoot('viewmodels/shell', 'entrance'); router.navigateTo('home'); } else { app.setRoot('viewmodels/public'); router.navigateTo('#/public'); } 

into the "activate" method on your public modem, and in main.js just leave this:

 app.setRoot('viewmodels/public'); 

EDIT: old sentence

I believe that for your view model for the root, you need a router property. Therefore, modify your public view model to add it:

 define(['durandal/app', 'durandal/plugins/router', 'services/dataservice'], function (app, router, dataservice) { var publicViewModel = function () { self.router = router; self.logIn = function () { app.setRoot('viewmodels/shell'); router.navigateTo('#/home'); }; 

(where do you define yourself?)

+1
source

All Articles