Skip to content

Instantly share code, notes, and snippets.

@fand
Created July 17, 2015 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fand/490bc2f5cc5f0b798d1e to your computer and use it in GitHub Desktop.
Save fand/490bc2f5cc5f0b798d1e to your computer and use it in GitHub Desktop.
Promiseベースの簡易ルータ
const EVENT = (pageID) => 'register:' + pageID
class Router extends EventEmitter {
constructor () {
this.routes = {};
}
getController (pageID) {
return new Promise((resolve, reject) => {
this.once(EVENT(pageID), function () {
this.routes[pageID](function () {
resolve(true);
});
});
});
}
selectRoute (pageID) {
var controller = this.getController(pageID);
if (this.routes[pageID]) {
this.emit(EVENT(pageID));
}
return controller();
}
register (pageID, controller) {
this.routes[pageID] = controller;
this.emit(EVENT(pageID));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment