How to define / use multiple routes using trunk and requirejs

I have divided my application into several applications.

main.js app.js app1/ |- routing |- controller |- app app2/ |- routing |- controller |- app 

1) When I try to use routers in app1 , they work.
2) When I try to use routers in app2 , they do not work.
3) If I comment on the line 'js/app1/routing', in main.js , the routers in app2 .

Why am I getting this behavior?
Is there an example application using multiple routes and requirejs for github?

thanks.

Here is my code:


** main.js **

 define([ 'js/app', 'js/app1/routing', // the routers in this app work 'js/app2/routing' // the routers in this app do not work but // if I comment the previous line (js/app1/routing',) // they works ], function (App) { "use strict"; App.initialize(); }); 

** app.js **

 define([], function () { "use strict"; var app = new Backbone.Marionette.Application(); return app; }); 

** app1 / rotuing **

 define(['backbone','app1/controller'], function(Backbone, controller) { "use strict"; var Router = Backbone.Marionette.AppRouter.extend({ appRoutes: { '*defaults': 'index1' } }); return new Router({ controller: controller }); }); 

** app2 / routing.js **

 define(['backbone','app2/controller'], function(Backbone, controller) { "use strict"; var Router = Backbone.Marionette.AppRouter.extend({ appRoutes: { 'app2': 'index2' } }); return new Router({ controller: controller }); }); 
+7
source share
1 answer

The problem is most likely caused by the download order of the router files and the creation of the routers.

The backbone history object is responsible for running the routes. It collects all the routes defined on all routers when the routers are created. He then controls the browser URL for changes. When he sees the change, he will take the first available route and start one route, skipping something else.

When you define the *defaults route, everything matches that. Therefore, if this route is loaded first, nothing else will suffer. Thus, you will need to be more explicit in the route parameters so that this one route does not hit all the time, or you need to make sure that this router is loaded last.

+5
source

All Articles