Simple router and route classes for Backbone.
Note: Backbone-routing requires a global
Promise
object to exist, please include aPromise
polyfill if necessary.
import {Route, Router} from 'backbone-routing';
const IndexRoute = Route.extend({
initialize(options) {
this.collection = options.collection;
},
fetch() {
return this.collection.fetch();
},
render() {
this.view = new View();
this.view.render();
},
destroy() {
this.view.remove();
}
});
const ShowRoute = Route.extend({
initialize(options) {
this.collection = options.collection;
},
fetch(id) {
this.model = this.collection.get(id);
if (!this.model) {
this.model = new Model({id});
return this.model.fetch();
}
},
render() {
this.view = new View({
model: this.model
});
},
destroy() {
this.view.remove();
}
});
const MyRouter = Router.extend({
initialize() {
this.collection = new Collection();
},
routes: {
'' : 'index',
':id' : 'show'
},
index() {
return new IndexRoute({
collection: this.collection
});
},
show() {
return new ShowRoute({
collection: this.collection
});
}
});
git clone git@github.com:thejameskyle/backbone-routing.git && cd backbone-routing
Make sure Node.js and npm are installed.
npm install
npm test
===
© 2015 James Kyle. Distributed under ISC license.