wip back btn

This commit is contained in:
Adam Bradley
2015-07-05 22:48:52 -05:00
parent db5b83de1a
commit 3d259725a3
2 changed files with 36 additions and 12 deletions

View File

@ -205,7 +205,7 @@ export function ionicBootstrap(ComponentType, config, router) {
bootstrap(ComponentType, injectableBindings).then(appRef => { bootstrap(ComponentType, injectableBindings).then(appRef => {
app.load(appRef); app.load(appRef);
router.init(); router.init(window);
// resolve that the app has loaded // resolve that the app has loaded
resolve(app); resolve(app);

View File

@ -36,7 +36,9 @@ export class IonicRouter {
} }
} }
init() { init(window) {
this.initHistory(window);
let rootViewCtrl = this.activeViewController(); let rootViewCtrl = this.activeViewController();
if (rootViewCtrl) { if (rootViewCtrl) {
let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise(); let matchedRoute = this.match( this.getCurrentPath() ) || this.otherwise();
@ -44,6 +46,36 @@ export class IonicRouter {
} }
} }
initHistory(window) {
this.location = window.location;
this.history = window.history;
window.addEventListener('popstate', (ev) => {
this.onPopState(ev);
});
}
onPopState(ev) {
let routeName = (ev.state && ev.state.name);
console.log('onPopState', routeName);
let activeViewCtrl = this.activeViewController();
if (activeViewCtrl) {
activeViewCtrl.pop();
}
}
pushState(route) {
let newPath = route.path;
if (this.location.hash !== '#' + newPath) {
let state = {
name: route.name
};
this.history.pushState(state, '', '#' + newPath);
}
}
match(path) { match(path) {
let matchedRoute = null; let matchedRoute = null;
let routeMatch = null; let routeMatch = null;
@ -101,20 +133,12 @@ export class IonicRouter {
let matchedRoute = this.match(routeConfig.path); let matchedRoute = this.match(routeConfig.path);
if (matchedRoute) { if (matchedRoute) {
this.updateState(matchedRoute); this.pushState(matchedRoute);
} }
} }
} }
} }
updateState(route) {
let newPath = route.path;
if (window.location.hash !== '#' + newPath) {
console.log('updateState', newPath);
window.location.hash = newPath;
}
}
addViewController(viewCtrl) { addViewController(viewCtrl) {
this._viewCtrls.push(viewCtrl); this._viewCtrls.push(viewCtrl);
} }
@ -127,7 +151,7 @@ export class IonicRouter {
} }
getCurrentPath() { getCurrentPath() {
let hash = window.location.hash; let hash = this.location.hash;
// Grab the path without the leading hash // Grab the path without the leading hash
return hash.slice(1); return hash.slice(1);
} }