Created
July 17, 2015 06:41
-
-
Save fand/490bc2f5cc5f0b798d1e to your computer and use it in GitHub Desktop.
Promiseベースの簡易ルータ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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