Last active
December 14, 2015 19:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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]); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Opened an issue with BackboneJS to support this directly.