Simple router mixin for React
var RouterMixin = {
respondTo: function(routes, root, pushState) {
var self = this;
if (!router) {
router = new Backbone.Router();
}
for (var route in routes) {
var name = 'route' + routeId++;
router.route(route, name, routes[route].bind(self));
}
if (!started) {
pushState = !!pushState;
Backbone.history.start({ root: root || '/', pushState: pushState });
started = true;
}
}
};
Example:
React.createClass({
mixin: [Router],
componentDidMount: function() {
this.respondTo({
'': this.fetchData,
'features/:id': this.fetchFeature
});
},
fetchData: function() {
...
},
fetchFeature: function(id) {
...
}
});
Credits:
Written by sydcanem
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Backbone
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#