2

Is it possible to close or stop a Backbone Router from listening to its defined routes?

I ask because I have been testing Backbone SubRoute (https://github.com/ModelN/backbone.subroute), setting up an application that has many spaces in which a user could have many subapplications running.

As such, I have defined a a main router, subrouter structure that follows roughy:

   MainRouter = Backbone.Router.extend
    routes:
      "":"root"
      "spaces/:id/:module(/*subroute)":"invokeModule"

    root: () ->
      console.log "root trigger"

    invokeModule: (id, module, subroute) ->
      that = this
      GigaApp.module(module).start({nested_root: "spaces/#{id}/#{module}"})

  SubAppRouter = Backbone.SubRoute.extend
    routes:
      "":"app_home"

    app_home: () ->
      console.log 'at sub app home'

  SubApp.on "start", (options) ->
    SubApp.router = new SubAppRouter(options.nested_root)

This general structure works from the first time a sub application is initialized for a space, as the MainRouter starts the SubApp, which initializes its router with the correct nested route. Subsequently, other routes defined in the SubAppRouter also trigger fine.

However, if you navigate to a different space (of different id), and navigate back to the first space, this structure breaks because the SubAppRouter already initialized for that space overrides the MainRouter, and no call the start the SubApp is made from the MainRouter.

So, I'm wondering if there is a way to stop or disable unbind the route triggering of a Backbone router.

jay
  • 12,066
  • 16
  • 64
  • 103

2 Answers2

2

As of this moment, the answer is NO

Derick Bailey opened this issue on Backbone's repo suggesting some changes to backbone's structure to support such a change: https://github.com/jashkenas/backbone/pull/1630

There followed a discussion of the merits of such a change, where the following point was made:

To get started -- I'm afraid that I don't understand the premise here. There are a couple axioms of routing that contradict this patch:

Routers exist to match URLs to locations in your application.

The whole point of URLs is that they're always reachable -- once you have a URL, you can go back to it (bookmark it, paste it into a browser) at any point.

I also went over the Backbone source code, and there is no indication that the functionality I am thinking about is possible.

jay
  • 12,066
  • 16
  • 64
  • 103
1

I'm curious why you're not using the AppRouter in Marionette? It should solve your problems regarding dividing routes into smaller AppRoute objects...

Have a look at BackboneRails for some screencasts on building large scale apps. It's not only relevant for devs using rails as backend. It gives a great way to layout your app structure in modules (each with their own App Routes). Highly recommendable.

Jakob Dam Jensen
  • 434
  • 5
  • 15
  • hi Jakob. Thanks for your reply, I will try out AppRouter, there is no reason I'm not using it yet other than that I am learning/trying Marionette for the first time. – jay Sep 23 '13 at 10:07
  • Fair enough =) - Let me know if I can help you in any way. It's a great lib and I really recommend watching the screencasts at BackboneRails. Helped me a lot and it gives you a way to structure your app. – Jakob Dam Jensen Sep 23 '13 at 11:56
  • Thanks, I will make time to watch them this week. The AppRouter is a nice, DRYer way (it seems) to run routing in a Marionette app, as it removes the need for several router actions that just function to call a single controller action. However, I am still thinking I need to have some nested routing - having sub-app routers listen to their routes while ignoring the base "spaces/:id" segment of the route. I will keep evaluating AppRouter though, maybe I am overcomplicating things. – jay Sep 24 '13 at 01:18