mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 03:32:21 +08:00
Routing with lifecycles
This commit is contained in:
@ -1,12 +1,13 @@
|
|||||||
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
|
import {Component, Directive, onInit} from 'angular2/src/core/annotations_impl/annotations';
|
||||||
import {View} from 'angular2/src/core/annotations_impl/view';
|
import {View} from 'angular2/src/core/annotations_impl/view';
|
||||||
import {ObservableWrapper} from 'angular2/src/facade/async';
|
|
||||||
|
|
||||||
import {Routable, Router, NavController, NavbarTemplate, Navbar, NavPush, Content} from 'ionic/ionic';
|
import {Routable, Router, NavController, NavbarTemplate, Navbar, NavPush, Content} from 'ionic/ionic';
|
||||||
import {SecondPage} from './second-page';
|
import {SecondPage} from './second-page';
|
||||||
|
|
||||||
|
@Component({
|
||||||
@Component({selector: 'ion-view'})
|
selector: 'ion-view',
|
||||||
|
lifecycle: [onInit]
|
||||||
|
})
|
||||||
@View({
|
@View({
|
||||||
template: '' +
|
template: '' +
|
||||||
'<ion-navbar *navbar>' +
|
'<ion-navbar *navbar>' +
|
||||||
@ -36,22 +37,18 @@ export class FirstPage {
|
|||||||
this.nav = nav;
|
this.nav = nav;
|
||||||
this.val = Math.round(Math.random() * 8999) + 1000;
|
this.val = Math.round(Math.random() * 8999) + 1000;
|
||||||
|
|
||||||
this.router = FirstPage.router.invoke(this);
|
|
||||||
|
|
||||||
this.pushPage = SecondPage;
|
this.pushPage = SecondPage;
|
||||||
this.pushData = {
|
this.pushData = {
|
||||||
id: 420
|
id: 420
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
onInit() {
|
||||||
console.log(this._viewDidEnter);
|
|
||||||
ObservableWrapper.subscribe(this._viewDidEnter, () => {
|
|
||||||
console.log('ENTERRRRR');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewLoaded() {
|
viewLoaded() {
|
||||||
|
this.router = FirstPage.router.invoke(this);
|
||||||
console.log('viewLoaded first page');
|
console.log('viewLoaded first page');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +58,6 @@ export class FirstPage {
|
|||||||
|
|
||||||
viewDidEnter() {
|
viewDidEnter() {
|
||||||
console.log('viewDidEnter first page');
|
console.log('viewDidEnter first page');
|
||||||
Router.emit(this.router.routeInfo.url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewWillLeave() {
|
viewWillLeave() {
|
||||||
|
@ -36,7 +36,6 @@ export class SecondPage {
|
|||||||
this.params = params;
|
this.params = params;
|
||||||
this.val = Math.round(Math.random() * 8999) + 1000;
|
this.val = Math.round(Math.random() * 8999) + 1000;
|
||||||
|
|
||||||
this.router = SecondPage.router.invoke(this);
|
|
||||||
|
|
||||||
console.log('Second page params:', params);
|
console.log('Second page params:', params);
|
||||||
}
|
}
|
||||||
@ -51,6 +50,7 @@ export class SecondPage {
|
|||||||
|
|
||||||
viewLoaded() {
|
viewLoaded() {
|
||||||
console.log('viewLoaded second page');
|
console.log('viewLoaded second page');
|
||||||
|
this.router = SecondPage.router.invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
viewWillEnter() {
|
viewWillEnter() {
|
||||||
@ -59,7 +59,6 @@ export class SecondPage {
|
|||||||
|
|
||||||
viewDidEnter() {
|
viewDidEnter() {
|
||||||
console.log('viewDidEnter second page');
|
console.log('viewDidEnter second page');
|
||||||
Router.emit(this.router.routeInfo.url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewWillLeave() {
|
viewWillLeave() {
|
||||||
|
@ -157,8 +157,10 @@ export class ViewItem {
|
|||||||
|
|
||||||
setInstance(instance) {
|
setInstance(instance) {
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
|
this.instance._viewItem = this;
|
||||||
|
|
||||||
this.instance._viewDidEnter = new EventEmitter('viewDidEnter');
|
this.instance._viewDidEnter = new EventEmitter('viewDidEnter');
|
||||||
|
this.instance._viewWillEnter = new EventEmitter('viewWillEnter');
|
||||||
}
|
}
|
||||||
|
|
||||||
cache() {
|
cache() {
|
||||||
@ -256,6 +258,7 @@ export class ViewItem {
|
|||||||
*/
|
*/
|
||||||
willEnter() {
|
willEnter() {
|
||||||
this.instance && this.instance.viewWillEnter && this.instance.viewWillEnter();
|
this.instance && this.instance.viewWillEnter && this.instance.viewWillEnter();
|
||||||
|
this.instance && this.instance._viewWillEnter.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -159,12 +159,19 @@ export class Routable {
|
|||||||
this.componentClass = componentClass;
|
this.componentClass = componentClass;
|
||||||
this.routeInfo = routeInfo;
|
this.routeInfo = routeInfo;
|
||||||
|
|
||||||
console.log('New routable', componentClass, routeInfo);
|
//console.log('New routable', componentClass, routeInfo);
|
||||||
|
|
||||||
componentClass.router = this;
|
componentClass.router = this;
|
||||||
}
|
}
|
||||||
invoke(componentInstance) {
|
invoke(componentInstance) {
|
||||||
console.log('Routable invoke', componentInstance);
|
// Called on viewLoaded
|
||||||
|
|
||||||
|
// Bind some lifecycle events
|
||||||
|
componentInstance._viewWillEnter.observer({
|
||||||
|
next: () => {
|
||||||
|
Router.emit(this.routeInfo.url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user