1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Route{ constructor(){ this.routes = {} this.currentHash = '' this.freshRoute = this.freshRoute.bind(this) window.addEventListener('load', this.freshRoute, false) window.addEventListener('hashchange', this.freshRoute, false) } storeRoute (path, cb) { this.routes[path] = cb || function () {} } freshRoute () { this.currentHash = location.hash.slice(1) || '/' this.routes[this.currentHash]() } }
|