fix(events): fix ionic/angular events

This commit is contained in:
Adam Bradley
2018-03-01 16:32:29 -06:00
parent 9c93df48d8
commit 79eddec369
5 changed files with 211 additions and 55 deletions

View File

@ -1,33 +1,20 @@
import { Injectable } from '@angular/core';
import { IonicWindow } from '../types/interfaces';
import { ensureElementInBody, hydrateElement } from '../util/util';
let hydratedElement: HTMLIonEventsElement = null;
@Injectable()
export class Events {
publish(topic: string, event?: any): Promise<any> {
return getElement().then((element: HTMLIonEventsElement) => {
return element.publish(topic, event);
});
}
subscribe(topic: string, handler: (event?: any) => void) {
return getElement().then((element: HTMLIonEventsElement) => {
return element.subscribe(topic, handler);
});
return (window as IonicWindow).Ionic.Events.subscribe(topic, handler);
}
}
function getElement(): Promise<HTMLIonEventsElement> {
if (hydratedElement) {
return Promise.resolve(hydratedElement);
}
const element = ensureElementInBody(ELEMENT_NAME);
return hydrateElement(element).then((element: HTMLIonEventsElement) => {
hydratedElement = element;
return element;
});
}
const ELEMENT_NAME = 'ion-events';
unsubscribe(topic: string, handler: (event?: any) => void) {
return (window as IonicWindow).Ionic.Events.unsubscribe(topic, handler);
}
publish(topic: string, event?: any) {
return (window as IonicWindow).Ionic.Events.publish(topic, event);
}
}

View File

@ -21,3 +21,16 @@ export interface AngularEscapeHatch extends EscapeHatch {
cfr?: ComponentFactoryResolver;
injector?: Injector;
}
export interface IonicGlobal {
config: any;
Events: {
subscribe: (topic: string, ...handlers: Function[]) => void;
unsubscribe: (topic: string, handler: Function) => void;
publish: (topic: string, ...args: any[]) => any[];
};
}
export interface IonicWindow extends Window {
Ionic: IonicGlobal;
}