router wip

This commit is contained in:
Adam Bradley
2015-06-29 15:59:50 -05:00
parent 5a77e0c5b9
commit 6f3dd4eb7d
7 changed files with 89 additions and 53 deletions

View File

@ -4,6 +4,7 @@ import {Compiler} from 'angular2/angular2';
import {ElementRef} from 'angular2/src/core/compiler/element_ref'; import {ElementRef} from 'angular2/src/core/compiler/element_ref';
import {bind} from 'angular2/di'; import {bind} from 'angular2/di';
import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref'; import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {IonicRouter} from '../../routing/router'; import {IonicRouter} from '../../routing/router';
import {IonicConfig} from '../../config/config'; import {IonicConfig} from '../../config/config';
@ -21,6 +22,34 @@ export class IonicApp {
this.components = {}; this.components = {};
} }
load(appRef) {
this.ref(appRef);
this.zone(this.injector().get(NgZone));
}
ref(val) {
if (arguments.length) {
this._ref = val;
}
return this._ref;
}
injector() {
return this._ref.injector;
}
zone(val) {
if (arguments.length) {
this._zone = val;
}
return this._zone;
}
stateChange() {
console.log('stage change');
}
/** /**
* Register a known component with a key, for easy lookups later. * Register a known component with a key, for easy lookups later.
*/ */
@ -46,7 +75,7 @@ export class IonicApp {
*/ */
appendComponent(ComponentType: Type) { appendComponent(ComponentType: Type) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let injector = this._ref.injector; let injector = this.injector();
let compiler = injector.get(Compiler); let compiler = injector.get(Compiler);
let viewMngr = injector.get(AppViewManager); let viewMngr = injector.get(AppViewManager);
let rootComponentRef = this._ref._hostComponent; let rootComponentRef = this._ref._hostComponent;
@ -80,13 +109,6 @@ export class IonicApp {
}); });
} }
ref(val) {
if (arguments.length) {
this._ref = val;
}
return this._ref;
}
applyCss(bodyEle, platform, config) { applyCss(bodyEle, platform, config) {
let className = bodyEle.className; let className = bodyEle.className;
@ -157,6 +179,7 @@ export function ionicBootstrap(ComponentType, config, router) {
// setup router // setup router
router = router || new IonicRouter(); router = router || new IonicRouter();
router.app(app);
// add injectables that will be available to all child components // add injectables that will be available to all child components
let injectableBindings = [ let injectableBindings = [
@ -166,7 +189,7 @@ export function ionicBootstrap(ComponentType, config, router) {
]; ];
bootstrap(ComponentType, injectableBindings).then(appRef => { bootstrap(ComponentType, injectableBindings).then(appRef => {
app.ref(appRef); app.load(appRef);
router.init(); router.init();

View File

@ -22,11 +22,11 @@ export function main(ionicBootstrap) {
}, },
'SecondPage': { 'SecondPage': {
'path': '/secondpage', 'path': '/secondpage',
'module': './second-page' 'module': 'dist/examples/nav/basic/pages/second-page',
}, },
'ThirdPage': { 'ThirdPage': {
'path': '/thirdpage', 'path': '/thirdpage',
'module': './third-page' 'module': 'dist/examples/nav/basic/pages/third-page',
}, },
}); });

View File

@ -1,12 +1,14 @@
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations'; import {IonicComponent, IonicView, IonicConfig, IonicApp} from 'ionic/ionic';
import {NavParams, NavController} from 'ionic/ionic';
import {IonicView, IonicConfig, IonicApp} from 'ionic/ionic';
import {NavParams, Routable, Router_OLD, NavController} from 'ionic/ionic';
import {SecondPage} from './second-page' import {SecondPage} from './second-page'
@Component({
selector: 'ion-view' @IonicComponent({
selector: 'ion-view',
route: {
path: '/firstpage'
}
}) })
@IonicView({ @IonicView({
template: '' + template: '' +

View File

@ -1,10 +1,13 @@
import {Component} from 'angular2/src/core/annotations_impl/annotations'; import {IonicComponent, IonicView, NavController, NavParams} from 'ionic/ionic';
import {IonicView, Routable, Router_OLD, NavController, NavParams} from 'ionic/ionic';
import {ThirdPage} from './third-page'; import {ThirdPage} from './third-page';
@Component({selector: 'ion-view'}) @IonicComponent({
selector: 'ion-view',
route: {
path: '/secondpage'
}
})
@IonicView({ @IonicView({
template: ` template: `
<ion-navbar *navbar><ion-title>Second Page Header</ion-title></ion-navbar> <ion-navbar *navbar><ion-title>Second Page Header</ion-title></ion-navbar>
@ -30,9 +33,6 @@ export class SecondPage {
nav: NavController, nav: NavController,
params: NavParams params: NavParams
) { ) {
// TODO: Shouldn't have to do this
Router_OLD.setNavController(nav);
this.nav = nav; this.nav = nav;
this.params = params; this.params = params;
this.val = Math.round(Math.random() * 8999) + 1000; this.val = Math.round(Math.random() * 8999) + 1000;
@ -50,7 +50,6 @@ export class SecondPage {
viewLoaded() { viewLoaded() {
console.log('viewLoaded second page'); console.log('viewLoaded second page');
this.router = SecondPage.router.invoke(this);
} }
viewWillEnter() { viewWillEnter() {
@ -87,6 +86,6 @@ export class SecondPage {
} }
new Routable(SecondPage, { // new Routable(SecondPage, {
url: '/second-page' // url: '/second-page'
}) // })

View File

@ -1,9 +1,12 @@
import {Component} from 'angular2/src/core/annotations_impl/annotations'; import {IonicComponent, IonicView, NavController} from 'ionic/ionic';
import {IonicView, Router_OLD, Routable, NavController} from 'ionic/ionic';
@Component({selector: 'ion-view'}) @IonicComponent({
selector: 'ion-view',
route: {
path: '/thirdpage'
}
})
@IonicView({ @IonicView({
template: ` template: `
<ion-navbar *navbar><ion-title>Third Page Header</ion-title></ion-navbar> <ion-navbar *navbar><ion-title>Third Page Header</ion-title></ion-navbar>
@ -20,9 +23,6 @@ export class ThirdPage {
nav: NavController nav: NavController
) { ) {
this.nav = nav this.nav = nav
// TODO: Shouldn't have to do this
Router_OLD.setNavController(nav);
} }
pop() { pop() {
@ -30,7 +30,6 @@ export class ThirdPage {
} }
viewLoaded() { viewLoaded() {
this.router = ThirdPage.router.invoke(this);
console.log('viewLoaded third page'); console.log('viewLoaded third page');
} }
@ -69,6 +68,6 @@ export class ThirdPage {
} }
new Routable(ThirdPage, { // new Routable(ThirdPage, {
url: '/third-page' // url: '/third-page'
}) // })

View File

@ -27,6 +27,7 @@ export class ViewController {
this.loader = injector.get(DynamicComponentLoader); this.loader = injector.get(DynamicComponentLoader);
this.viewMngr = injector.get(AppViewManager); this.viewMngr = injector.get(AppViewManager);
this.router = injector.get(IonicRouter); this.router = injector.get(IonicRouter);
this.app = injector.get(IonicApp);
this.router.addViewController(this); this.router.addViewController(this);
@ -308,6 +309,8 @@ export class ViewController {
} }
}); });
this.app.stateChange();
// allow clicks again // allow clicks again
ClickBlock(false); ClickBlock(false);
} }

View File

@ -8,6 +8,10 @@ export class IonicRouter {
this.config(config); this.config(config);
} }
app(app) {
this._app = app;
}
config(config) { config(config) {
for (let routeName in config) { for (let routeName in config) {
this.addRoute(routeName, config[routeName]); this.addRoute(routeName, config[routeName]);
@ -15,8 +19,10 @@ export class IonicRouter {
} }
addRoute(name, routeConfig) { addRoute(name, routeConfig) {
let route = new IonicRoute(name, routeConfig); if (name && routeConfig && routeConfig.path) {
this._routes.push(route); let route = new IonicRoute(name, routeConfig);
this._routes.push(route);
}
} }
init() { init() {
@ -54,31 +60,35 @@ export class IonicRouter {
} }
push(viewCtrl, route) { push(viewCtrl, route) {
let self = this;
function run() {
self._app.zone().run(() => {
viewCtrl.push(route.cls).then(() => {
self.updateState(route);
});
});
}
if (viewCtrl && route) { if (viewCtrl && route) {
if (route.cls) { if (route.cls) {
viewCtrl.push(route.cls).then(() => { run();
this.pushCompleted(route);
});
} else if (route.module) { } else if (route.module) {
System.import(route.module).then((m) => { System.import(route.module).then(m => {
if (m) { if (m) {
route.cls = m[route.name]; route.cls = m[route.name];
viewCtrl.push(route.cls).then(() => { run();
this.pushCompleted(route);
});
} }
}); });
} }
} }
} }
pushCompleted(route) { updateState(activeRoute) {
console.log('pushCompleted', route); console.log('updateState', activeRoute);
}
updateState(route) {
window.location.hash = activeRoute.path;
} }
addViewController(viewCtrl) { addViewController(viewCtrl) {