Reputation: 2158
How do I use custom events in TypeScript?
In jQuery I do this:
$("#btnShowExplorer").click(function () {
$.event.trigger("showexplorerpf");
});
and somewhere else I 'listen' for that event
// bind to the special custom event
$('#idExplorerWindow').bind("showexplorerpf", function () {
// do stuff...
});
I am now moving my code to TypeScript - I have a ref to the jQuery type def file but the $.event.trigger is not recognized..
Thanks
Upvotes: 2
Views: 5793
Reputation: 561
Actually trigger() has multiple signatures.
Check http://api.jquery.com/trigger/
For me this works better as I need to send some data with event:
interface JQueryEvent {
trigger(name: string, data:any[]): void;
}
interface JQueryStatic {
event: JQueryEvent;
}
Upvotes: 0
Reputation: 251242
You may need to extend the jQuery interface. My example has it all in one file, but it doesn't have to be in the same file - just one you reference.
interface JQueryEvent {
trigger(name: string): void;
}
interface JQueryStatic {
event: JQueryEvent;
}
$("#btnShowExplorer").click(function () {
$.event.trigger("showexplorerpf");
});
Upvotes: 1