d4f / backbone-highway

Routing Backbone with style \o/
MIT License
19 stars 5 forks source link

Passing arguments to aliased routes #9

Closed ghost closed 9 years ago

ghost commented 9 years ago

When declaring an alias to another route via its path the primary route arguments are not passed in. For example :

Backbone.Highway.map(function () {
  this.route('user', {
    path: '/user/:id',
    action: function (id) { /* Render user page */ }
  });

  this.route('user.edit', {
    path: '/user/:id/edit',
    before: [
      {path: '/user/$1'} // $1 should be filled in with :id
    ],
    action: function (id) { /* Render user edit page */ }
  });
});
ghost commented 9 years ago

Fixed this in a different manner.

Backbone.Highway.map(function () {
  this.route('user', {
    path: '/user/:id',
    action: function (id) { /* Render user page */ }
  });

  this.route('user.edit', {
    path: '/user/:id/edit',
    before: [
      // will automatically receive user.edit route argument
      'user'

      // will override argument received from parrent route
      {name: 'user', args: [1234]}

      // here the argument gets extracted and also overrides parent route argument
      {path: '/user/1234'}
    ],
    action: function (id) { /* Render user edit page */ }
  });
});