From db6c7b728332b65b359bb2b49e6e531b04a900b6 Mon Sep 17 00:00:00 2001 From: Tim Lancina Date: Thu, 24 Sep 2015 17:34:47 -0600 Subject: [PATCH] feat(nav): add NavRegistry --- ionic/components/app/app.ts | 7 +++++-- ionic/components/nav/nav-push.ts | 18 +++++++++++++++--- ionic/components/nav/nav-registry.ts | 17 +++++++++++++++++ ionic/components/nav/test/basic/index.ts | 9 ++++++--- ionic/config/decorators.ts | 2 +- 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 ionic/components/nav/nav-registry.ts diff --git a/ionic/components/app/app.ts b/ionic/components/app/app.ts index 4d27e136bd..def11107a2 100644 --- a/ionic/components/app/app.ts +++ b/ionic/components/app/app.ts @@ -13,6 +13,7 @@ import {Modal} from '../modal/modal'; import {Popup} from '../popup/popup'; import {FocusHolder} from '../form/focus-holder'; import {Events} from '../../util/events'; +import {NavRegistry} from '../nav/nav-registry'; /** * @name IonicApp @@ -278,7 +279,7 @@ function initApp(window, document, config, platform) { * @param {TODO} config TODO * @return {Promise} TODO */ -export function ionicBootstrap(rootComponentType, config) { +export function ionicBootstrap(rootComponentType, views, config) { return new Promise(resolve => { try { // get the user config, or create one if wasn't passed in @@ -297,6 +298,7 @@ export function ionicBootstrap(rootComponentType, config) { let modal = new Modal(app, config); let popup = new Popup(app, config); let events = new Events(); + let navRegistry = new NavRegistry(views); // add injectables that will be available to all child components let appBindings = Injector.resolve([ @@ -309,7 +311,8 @@ export function ionicBootstrap(rootComponentType, config) { bind(Popup).toValue(popup), bind(Events).toValue(events), ROUTER_BINDINGS, - bind(LocationStrategy).toClass(HashLocationStrategy) + bind(LocationStrategy).toClass(HashLocationStrategy), + bind(NavRegistry).toValue(navRegistry) ]); bootstrap(rootComponentType, appBindings).then(appRef => { diff --git a/ionic/components/nav/nav-push.ts b/ionic/components/nav/nav-push.ts index c592978998..2646ff3004 100644 --- a/ionic/components/nav/nav-push.ts +++ b/ionic/components/nav/nav-push.ts @@ -1,5 +1,6 @@ import {Directive} from 'angular2/angular2'; import {NavController} from './nav-controller'; +import {NavRegistry} from './nav-registry'; /** * TODO @@ -20,19 +21,30 @@ export class NavPush { * TODO * @param {NavController} nav TODO */ - constructor(nav: NavController) { + constructor(nav: NavController, registry: NavRegistry) { this.nav = nav; + this.registry = registry; } onClick(event) { + let destination, params; + if (this.instruction instanceof Array) { if (this.instruction.length > 2) { throw 'Too many [nav-push] arguments, expects [View, { params }]' } - this.nav.push(this.instruction[0], this.instruction[1]); + destination = this.instruction[0]; + params = this.instruction[1] || this.params; } else { - this.nav.push(this.instruction, this.params); + destination = this.instruction; + params = this.params; } + + if (typeof destination === "string") { + destination = this.registry.get(destination); + } + + this.nav.push(destination, params); } } diff --git a/ionic/components/nav/nav-registry.ts b/ionic/components/nav/nav-registry.ts new file mode 100644 index 0000000000..d7d8e43300 --- /dev/null +++ b/ionic/components/nav/nav-registry.ts @@ -0,0 +1,17 @@ + +/** + * Map of possible views that can be navigated to using an Ionic NavController + */ +export class NavRegistry { + constructor(views) { + this._views = new Map(views.map(view => [view.name, view])); + } + + get(viewName) { + return this._views.get(viewName); + } + + set(view) { + this._views.set(view.name, view); + } +} diff --git a/ionic/components/nav/test/basic/index.ts b/ionic/components/nav/test/basic/index.ts index 7f0c1a7021..9960b495ac 100644 --- a/ionic/components/nav/test/basic/index.ts +++ b/ionic/components/nav/test/basic/index.ts @@ -17,8 +17,10 @@ import {NavParams, NavController} from 'ionic/ionic'; '' + '

{{title}}

' + '

' + - '

' + - '

' + + '

' + + '

' + + '

' + + '

' + '

' + '' + '' + @@ -127,7 +129,8 @@ class ThirdPage { @App({ - template: '' + template: '', + views: [FirstPage, SecondPage, ThirdPage] }) class E2EApp { constructor() { diff --git a/ionic/config/decorators.ts b/ionic/config/decorators.ts index 4d693c717f..0b28b96fcb 100644 --- a/ionic/config/decorators.ts +++ b/ionic/config/decorators.ts @@ -180,7 +180,7 @@ export function App(args={}) { // redefine with added annotations Reflect.defineMetadata('annotations', annotations, cls); - ionicBootstrap(cls, args.config); + ionicBootstrap(cls, args.views, args.config); return cls; }