/backbone-routing

Simple router and route classes for Backbone.

Primary LanguageJavaScriptISC LicenseISC

Backbone Routing

Simple router and route classes for Backbone.

Travis build status Code Climate Test Coverage Dependency Status devDependency Status

Usage

Note: Backbone-routing requires a global Promise object to exist, please include a Promise 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
    });
  }
});

Contibuting

Getting Started

Fork and clone this repo.

git clone git@github.com:thejameskyle/backbone-routing.git && cd backbone-routing

Make sure Node.js and npm are installed.

npm install

Running Tests

npm test

===

© 2015 James Kyle. Distributed under ISC license.