Search code examples
javascriptbackbone.jscode-organizationbackbone-routing

Backbonejs: Organizing Routes


How do yo organize your route object if there's too much routes inside it, let's say a hundred. Do you just put it in a single file or object?

Thanks.


Solution

  • I think there's no right or wrong answer here. What's definitely true is that you cannot have a single router with 50+ routes in it...that's a maintainability nightmare.

    Instead, I prefer to split up my application into sub-applications or modules (call them as you want), where each of them has a well defined purpose and responsibility. As such, in a hypothetical order management system I'd potentially have

    • order mgmt app
      • person app
        • router.js
        • app.js
        • ...
      • order app
        • router.js
        • app.js
        • ...
      • ...
      • globalrouter.js
      • globalapp.js

    The key here is simply to never create a huge app/monolith, but instead create many small apps which can be composed together to form the entire application (communicating with events between each of them). That said, in the example above, the "person" as well as the "order" app have their own routers which just hold the routes relevant for them. The "global" app forms the glue, holding all of the single apps together, like having the app menu and maybe also some generic routes, not tight to a specific module/subapp.

    Maybe Backbone Aura is a good point to start.