Skip to content

Instantly share code, notes, and snippets.

@fand fand/router.js
Created Jul 17, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.