promise based getCurrentPath()

This commit is contained in:
Adam Bradley
2015-07-09 12:25:34 -05:00
parent 45ac09a391
commit 1427c254c5
3 changed files with 69 additions and 47 deletions

View File

@ -205,10 +205,10 @@ export function ionicBootstrap(cls, config, router) {
bootstrap(cls, injectableBindings).then(appRef => { bootstrap(cls, injectableBindings).then(appRef => {
app.load(appRef); app.load(appRef);
router.load(app, config, window); router.load(window, app, config).then(() => {
// resolve that the app has loaded // resolve that the app has loaded
resolve(app); resolve(app);
});
}).catch(err => { }).catch(err => {
console.error('ionicBootstrap', err); console.error('ionicBootstrap', err);

View File

@ -4,12 +4,10 @@ import * as util from '../util/util';
class HashUrlStateManager { class HashUrlStateManager {
constructor(router, ionicApp, ionicConfig, window) { constructor(window, router) {
this.router = router;
this.ionicApp = ionicApp;
this.ionicConfig = ionicConfig;
this.location = window.location; this.location = window.location;
this.history = window.history; this.history = window.history;
this.router = router;
window.addEventListener('popstate', ev => { window.addEventListener('popstate', ev => {
this.onPopState(ev); this.onPopState(ev);
@ -80,7 +78,7 @@ class HashUrlStateManager {
} else if (newStateBackPath === lastLoadedStatePath) { } else if (newStateBackPath === lastLoadedStatePath) {
// if the last loaded state path is the new state's // if the last loaded state path is the new state's
// back path, then the user is moving forward // back path, then the user is moving forward
this.loadByPath(newStatePath); this.router.loadByPath(newStatePath);
} }
} }
@ -88,10 +86,12 @@ class HashUrlStateManager {
getCurrentPath() { getCurrentPath() {
// Grab the path without the leading hash // Grab the path without the leading hash
return { return new Promise(resolve => {
resolve({
path: this.location.hash.slice(1), path: this.location.hash.slice(1),
priority: 0 priority: 0
} })
});
} }
isDifferentPath(path) { isDifferentPath(path) {

View File

@ -32,17 +32,22 @@ export class IonicRouter {
} }
} }
load(ionicApp, ionicConfig, window) { load(window, ionicApp, ionicConfig) {
// create each of the state manager classes // create each of the state manager classes
for (let name in stateManagerClasses) { for (let name in stateManagerClasses) {
stateManagers[name] = new stateManagerClasses[name](this, ionicApp, ionicConfig, window); stateManagers[name] = new stateManagerClasses[name](window, this, ionicApp, ionicConfig);
} }
stateManagerClasses = {}; stateManagerClasses = {};
this.loadByPath(this.getCurrentPath(), this.otherwise()); return new Promise(resolve => {
this.getCurrentPath().then(path => {
this.loadByPath(path, this.otherwise()).then(resolve);
});
});
} }
loadByPath(path, fallbackRoute) { loadByPath(path, fallbackRoute) {
return new Promise(resolve => {
let self = this; let self = this;
let activeViewCtrl = self.activeViewController(); let activeViewCtrl = self.activeViewController();
let matchedRoute = self.match(path) || fallbackRoute; let matchedRoute = self.match(path) || fallbackRoute;
@ -51,6 +56,9 @@ export class IonicRouter {
self._app.zone().run(() => { self._app.zone().run(() => {
activeViewCtrl.push(matchedRoute.cls); activeViewCtrl.push(matchedRoute.cls);
self.lastPath(matchedRoute.path); self.lastPath(matchedRoute.path);
resolve();
}, err => {
console.error(err);
}); });
} }
@ -65,26 +73,40 @@ export class IonicRouter {
matchedRoute.cls = m[matchedRoute.name]; matchedRoute.cls = m[matchedRoute.name];
zoneLoad(); zoneLoad();
} }
}, err => {
console.error(err);
}); });
} }
} }
});
} }
getCurrentPath() { getCurrentPath() {
// check each of the state managers and the one with the // check each of the state managers and the one with the
// highest priority wins of knowing what path we are currently at // highest priority wins of knowing what path we are currently at
return new Promise(resolve => {
let promises = [];
for (let name in stateManagers) {
promises.push(stateManagers[name].getCurrentPath());
}
// when all the promises have resolved then see which one wins
Promise.all(promises).then(results => {
let rtnPath = null; let rtnPath = null;
let highestPriority = -1; let highestPriority = -1;
let currentState = null; let state = null;
for (let name in stateManagers) { for (let i = 0; i < results.length; i++) {
currentState = stateManagers[name].getCurrentPath(); state = results[i];
if (currentState.path && currentState.priority > highestPriority) { if (state.path && state.priority > highestPriority) {
rtnPath = currentState.path; rtnPath = state.path;
} }
} }
return rtnPath; resolve(rtnPath);
});
});
} }
stateChange(type, activeView) { stateChange(type, activeView) {