Backbone router not recognized

42 views Asked by At

I am trying to create a Router for backbone, and I want to write it as a class with es6, so I have come up with this as a test:

class MyRouter extends Backbone.Router {
  constructor() {
    super(arguments);
  }

  routes = {
    "*path": "error"
  }

  error(path) {
    console.log("in myRouter error")
    this.trigger("component", {
      content: require("./error404/")(path)
    });
    return this.setRoute("error");
  }
}

module.exports = new MyRouter();

However for some reason it is not recognized by Backbone, so the question is, am I missing something? Do I have to tell backbone to use my specific router?

1

There are 1 answers

0
munHunger On

Turns out the error was quite simple. You need to call super with the routes, i.e.

class MyRouter extends Backbone.Router {
  constructor(options) {
    super({ routes: { "*path": "error" }, ...options });
  }

  initialize() {
    this.prev = undefined;
  }

  setRoute(route) {
    this.prev = route;
  }

  error(path) {
    this.trigger("component", {
      content: require("./error404/")(path)
    });
    return this.setRoute("error");
  }
}

let router = new MyRouter();
module.exports = router;