Gary Iams
Gary Iams

Reputation: 55

Route collision between Marionette Apps. Possible ideas/solutions?

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:

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.

Upvotes: 1

Views: 444

Answers (1)

ajk
ajk

Reputation: 4603

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).

Upvotes: 2

Related Questions