Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup multiple routers and controllers in Backbone.Marionette

Tags:

marionette

As my application grows I feel the need for a more structured router/controller setup. The Marionette docs mention the following:

It is recommended that you divide your controller objects into smaller pieces of related functionality and have multiple routers / controllers, instead of just one giant router and controller.

It would be great if there would be an example for this kind of structure. Suppose I have a website consisting of three sections; People, Media and Articles. Ideally I would like to have routers/controllers for each section. Something like this:

App
  -- AppRouter
       -- AppController
            -- AppLayout
            -- PeopleRouter
               -- PeopleController
                  -- PeopleLayout
            -- MediaRouter
               -- MediaController
                  -- MediaLayout
            -- ArticleRouter
               -- ArticleController
                  -- ArticleLayout

What's very unclear for me is the way how I can let a route ("/people/detail/1") be handled by another router than the AppRouter. It seems that I have to create modules for every section, but does this work nicely with RequireJS? It seems a bit weird to use two different kinds of modules...

It would be great if someone could point me in the right direction, all the examples I found online are a bit confusing to me.

like image 363
thomasjonas Avatar asked Nov 05 '13 09:11

thomasjonas


1 Answers

Take a look at the example application developed in my Marionette book.

You can see 2 router files:

  • https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/about/about_app.js
  • https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/contacts_app.js

If you want to learn more about the routing approach used in the book, there's a free chapter on the subject in the sample. Although it doesn't explain the various router files, it explains the idea of separating app state management from URL management; which might be of interest to you.

Hope this helps !

like image 92
David Sulc Avatar answered Nov 07 '22 13:11

David Sulc