mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 01:52:19 +08:00
fix(events): fix ionic/angular events
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user