Question

I am in the middle of building a Marionette application and just got bit by a route collision. Being fairly new to Backbone, I am unsure of whether or not it is possible to create a {black,white}list for Marionette's AppRouter. The best leads I have seen so far are:

  • Implement a Default route.
    • I don't believe this will solve my problem because these Apps are in two separate modules.
  • Integrate regex-based routing as an extension to Marionette.AppRouter.
    • This does not strike me as the simplest thing that could possibly work.

Before I layout what is happening, here is some context:

users_app.js.coffee:

@Gdit.module "UsersApp", (UsersApp, App, Backbone, Marionette, $, _) ->

  class UsersApp.Router extends Marionette.AppRouter
    appRoutes:
      'users/:id/edit': 'editUser'
      'users/:id': 'showUser'

  API =
    showUser: (id) ->
      new UsersApp.Show.Controller
        id: id

    editUser: (id) ->
      new UsersApp.Edit.Controller
        id: id

  App.addInitializer ->
    new UsersApp.Router
      controller: API

roles_app.js.coffee:

@Gdit.module "RolesApp", (RolesApp, App, Backbone, Marionette, $, _) ->

  class RolesApp.Router extends Marionette.AppRouter
    appRoutes:
      'users/roles': 'editUsersRoles'

  API =
    editUsersRoles: ->
      new RolesApp.Edit.Controller

  App.addInitializer ->
    new RolesApp.Router
      controller: API

The problem arises when I try to access the Roles.Edit.Controller through /#users/roles:

Started GET "/users/roles" for 127.0.0.1 at 2013-08-16 14:02:01 -0400
Processing by UsersController#show as JSON
  Parameters: {"id"=>"roles"}

As you can see, "roles" is being captured by the /users/:id route and being processed as an id. Not what I expected :)

Lastly, since I mentioned the "simplest thing that could possibly" work, I acknowledge that that would, in fact, be to just change the route for editUsersRoles to /users_roles. However I am more curious to see if a more elegant solution exists and see some spikes, etc. for it.

Was it helpful?

Solution

As I understand it, Backbone stops at the first route pattern that matches a URL. So if you run addInitializer() for your Roles router first, it will get first crack at matching the URL.

That said, setup of that Roles router seems likely to cause maintenance problems (or at least confusion). It would be more natural to have the Roles router match on /roles, /roles/:userId, etc (or even /users_roles as you mentioned).

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