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 209 additions and 53 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 (window as IonicWindow).Ionic.Events.subscribe(topic, handler);
}
subscribe(topic: string, handler: (event?: any) => void) {
return getElement().then((element: HTMLIonEventsElement) => {
return element.subscribe(topic, handler);
});
unsubscribe(topic: string, handler: (event?: any) => void) {
return (window as IonicWindow).Ionic.Events.unsubscribe(topic, handler);
}
}
function getElement(): Promise<HTMLIonEventsElement> {
if (hydratedElement) {
return Promise.resolve(hydratedElement);
publish(topic: string, event?: any) {
return (window as IonicWindow).Ionic.Events.publish(topic, event);
}
const element = ensureElementInBody(ELEMENT_NAME);
return hydrateElement(element).then((element: HTMLIonEventsElement) => {
hydratedElement = element;
return element;
});
}
const ELEMENT_NAME = 'ion-events';
}