chore(id): finding component by an id attr has been removed

BREAKING CHANGES: app.getComponent() has been removed. Please use
Angular's ViewChild annotation instead:
http://learnangular2.com/viewChild/
This commit is contained in:
Adam Bradley
2016-04-28 11:29:48 -05:00
parent 4fca31ec36
commit df328361fd
4 changed files with 7 additions and 139 deletions

View File

@@ -1,5 +1,4 @@
export * from './components/app/app';
export * from './components/app/id';
export * from './components/action-sheet/action-sheet';
export * from './components/alert/alert';
export * from './components/badge/badge';

View File

@@ -12,7 +12,6 @@ import {Platform} from '../../platform/platform';
*/
@Injectable()
export class IonicApp {
private _cmps: {[id: string]: any} = {};
private _disTime: number = 0;
private _scrollTime: number = 0;
private _title: string = '';
@@ -145,59 +144,20 @@ export class IonicApp {
/**
* @private
* Register a known component with a key, for easy lookups later.
* @param {string} id The id to use to register the component
* @param {object} component The component to register
*/
register(id: string, component: any) {
this._cmps[id] = component;
}
/**
* @private
* Unregister a known component with a key.
* @param {string} id The id to use to unregister
*/
unregister(id: string) {
delete this._cmps[id];
}
/**
* @private
* Get a registered component with the given type (returns the first)
* @param {object} cls the type to search for
* @return {object} the matching component, or undefined if none was found
*/
getRegisteredComponent(cls: any): any {
for (let key in this._cmps) {
const component = this._cmps[key];
if (component instanceof cls) {
return component;
}
}
// deprecated warning: added 2016-04-28, beta7
console.warn('Using app.getRegisteredComponent() to query components has been deprecated. ' +
'Please use Angular\'s ViewChild annotation instead:\n\nhttp://learnangular2.com/viewChild/');
}
/**
* Get the component for the given key.
* @private
*/
getComponent(id: string): any {
// deprecated warning
if (/menu/i.test(id)) {
console.warn('Using app.getComponent(menuId) to control menus has been deprecated as of alpha55.\n' +
'Instead inject MenuController, for example:\n\n' +
'constructor(menu: MenuController) {\n' +
' this.menu = menu;\n' +
'}\n' +
'toggleMenu() {\n' +
' this.menu.toggle();\n' +
'}\n' +
'openRightMenu() {\n' +
' this.menu.open("right");\n' +
'}'
);
}
return this._cmps[id];
// deprecated warning: added 2016-04-28, beta7
console.warn('Using app.getComponent() to query components has been deprecated. ' +
'Please use Angular\'s ViewChild annotation instead:\n\nhttp://learnangular2.com/viewChild/');
}
/**

View File

@@ -1,89 +0,0 @@
import {AppViewManager, ElementRef, Directive, Renderer, Input} from 'angular2/core';
import {IonicApp} from './app';
/**
* @private
* @name Id
* @description
* The `id` attribute is an easy way to identify unique components in an app and access them
* no matter where in the UI hierarchy you are. For example, this makes toggling
* a global side menu possible from any place in the application.
*
* @usage
* To give any component an ID, simply set its `id` property:
* ```html
* <ion-checkbox id="myCheckbox"></ion-checkbox>
* ```
*
* To get a reference to the registered component, inject the [IonicApp](../IonicApp/)
* service:
* ```ts
* constructor(app: IonicApp) {
* this.app = app
* }
*
* ngAfterViewInit() {
* var checkbox = this.app.getComponent("myCheckbox");
* if (checkbox.checked) {
* console.log('checkbox is checked');
* }
* }
* ```
*
* *NOTE:* It is not recommended to use ID's across Pages, as there is often no
* guarantee that the registered component has not been destroyed if its Page
* has been navigated away from.
*
* @demo /docs/v2/demos/id/
*/
@Directive({
selector: '[id]'
})
export class IdRef {
private _component: any;
/**
* @private
*/
@Input() id: string;
constructor(private _app: IonicApp, elementRef: ElementRef, appViewManager: AppViewManager) {
// Grab the component this directive is attached to
this._component = appViewManager.getComponent(elementRef);
}
/**
* @private
*/
ngOnInit() {
this._app.register(this.id, this._component);
}
/**
* @private
*/
ngOnDestroy() {
this._app.unregister(this.id);
}
}
/**
* @private
*/
@Directive({
selector: '[attr]'
})
export class Attr {
@Input() attr: string;
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
/**
* @private
*/
ngOnInit() {
this._renderer.setElementAttribute(this._elementRef.nativeElement, this.attr, '');
}
}

View File

@@ -38,7 +38,6 @@ import {Nav} from '../components/nav/nav';
import {NavPush, NavPop} from '../components/nav/nav-push';
import {NavRouter} from '../components/nav/nav-router';
import {NavbarTemplate, Navbar} from '../components/navbar/navbar';
import {IdRef} from '../components/app/id';
import {ShowWhen, HideWhen} from '../components/show-hide-when/show-hide-when';
/**
@@ -211,7 +210,6 @@ export const IONIC_DIRECTIVES = [
NavPush,
NavPop,
NavRouter,
IdRef,
ShowWhen,
HideWhen