Ticket #3662 (closed feature: fixed)
Add an Event class to jQuery
| Reported by: | flesler | Owned by: | flesler |
|---|---|---|---|
| Priority: | major | Milestone: | 1.3 |
| Component: | event | Version: | 1.2.6 |
| Keywords: | Cc: | john, brandon, joern, davidserduke | |
| Blocking: | Blocked by: |
Description
While it's possible to pass custom event objects to jQuery.trigger, it's pretty complicated and lengthy to do that.
Also, there's no way to add custom attributes to the event objects when triggering (binding data is the closest).
In addition, this class could be used by both jQuery.event.fix and jQuery.trigger. Instead of creating literal objects with empty or custom methods (preventDefault,etc) we could have those in a prototype. That's much lighter than thousands of lambda functions.
This change breaks 2 old behaviors (on purpose).
- It's not possible to pass an event object in the data array. It has to be passed as first argument, instead of the type, its 'type' will be used.
- extra functions passed to jQuery.trigger won't get the event object (they used to get it if it was on the data array).
These are the new behaviors allowed:
$('#foo').trigger( new jQuery.Event('click') );
var e = new jQuery.Event('foo');
e.customAttr = 'bar';
$('#foo').trigger( e );
Attachments
Change History
comment:1 Changed 4 years ago by flesler
- need changed from Patch to Commit
- Status changed from new to assigned
comment:2 Changed 4 years ago by flesler
By the way, this is the native behavior of AS3 and probably EcmaScript something.
An event object is passed to dispatchEvent (trigger), not the type. I think it's nice if we get closer to the standards.
Note that we already added event.stopImmediatePropagation().
Changed 4 years ago by flesler
-
attachment
event-object[5997].diff
added
Updated to new changes. Supports object literals as event objects (they're then copied to an actual instance)
Changed 4 years ago by flesler
-
attachment
event-object[6004].diff
added
This version makes all the tests pass, adds currentTarget attribute to event objects
comment:3 Changed 4 years ago by flesler
- Status changed from assigned to closed
- Resolution set to fixed
Applied at [6005].
comment:4 Changed 4 years ago by flesler
Since [6008], jQuery.Event can be instantiated without the 'new' operator, still optional.
comment:5 Changed 4 years ago by flesler
- event objects have isDefaultPrevented, isPropagationStopped and isImmediatePropagationStopped methods.
- Removed donative and dohandlers from $.trigger
- event.result carries the last returned data from a handler(not undefined).
- $.trigger doesn't keep bubbling if stopPropagation() is called.
- Same event object is used all along for global trigger
- Fixed the bug where target doesn't change when triggering on many elements.
- The data array on $.trigger was accumulating objects as it bubbles.
- adding more tests
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
