Skip to content

Instantly share code, notes, and snippets.

@erichiggins
Last active December 14, 2015 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichiggins/5140675 to your computer and use it in GitHub Desktop.
Save erichiggins/5140675 to your computer and use it in GitHub Desktop.
Backbone.Router mixin to bind optional route mappings. This would be useful for user-only or admin-only routes that would 404 otherwise.
/**
* Usage:
* var MyRouter = Backbone.Router.extend(_.extend({}, OptionalRouteMixin, {
* userRoutes: {
* '/profile': 'profile'
* },
* initialize: function(options) {
* this._bindOptRoutes(this.userRoutes);
* }
* }));
*/
var OptionalRouteMixin = {
// The following is borrowed from Backbone.Router._bindRoutes.
_bindOptRoutes: function(optRoutes) {
if (!optRoutes) return;
var route, routes = _.keys(optRoutes);
while ((route = routes.pop()) != null) {
this.route(route, optRoutes[route]);
}
}
};
@erichiggins
Copy link
Author

Opened an issue with BackboneJS to support this directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment