This is useful when jQuery custom events aren’t appropriate because there’s no DOM element involved.
This is implemented as a thin wrapper on jQuery.Callbacks().
Usage
Here’s a simple class Car that exposes the event ondrive:
varCar=function(){$.CustomEvent.create(this,"ondrive");this.drive=function(){// TODO some driving implementationthis.ondrive.fire();}}
A consumer of Car could observe the event:
varcar=newCar();car.ondrive.add(function(){alert("my car is driving!");});
We can also customize the event object that is passed to observers:
varCar=function(){$.CustomEvent.create(this,"ondrive");this.drive=function(speed){// Data will be passed to handlers via the event objectthis.ondrive.fire({speed:speed});}}varcar=newCar();car.ondrive.add(function(e){alert("my car is driving at "+e.speed+"mph!");});
The event object passed to handlers of jQuery.CustomEvent is a jQuery.Event, and is
also returned from jQuery.CustomEvent.fire(). This is useful if you want to use jQuery.Event’s methods,
such as preventDefault() and isDefaultPrevented() to allow observers to influence the behavior of the
object.