Dispatching and listening for application events in Typescript
npm install event-dispatchnpm install event-dispatch --save
typings install
npm install es6-shim --save
require("es6-shim"); in your app.
on your page.
javascript
import {EventSubscriber, On} from "event-dispatch";
@EventSubscriber()
export class UserEventSubscriber {
@On("onUserCreate")
onUserCreate(user: User) {
console.log("User " + user.name + " created!");
}
@On("onStatusUpdate")
updateUserStatus(status: string) {
console.log("New status: " + status);
}
}
`
Then use EventDispatcher class to dispatch events:
`javascript
import {EventDispatcher} from "event-dispatch";
// note that all your subscribers must be imported somewhere in the app, so they are getting registered
// on node you can also require the whole directory using require all package
import "./subscriber/UserEventSubscriber";
let eventDispatcher = new EventDispatcher();
eventDispatcher.dispatch("onUserCreate", new User("Johny"));
eventDispatcher.dispatch("onStatusUpdate", "hello world");
``