Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top