Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#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
Blocked by: Blocking:

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 (3)

event-object[5953].diff (12.1 KB) - added by flesler 12 years ago.
event-object[5997].diff (10.1 KB) - added by flesler 12 years ago.
Updated to new changes. Supports object literals as event objects (they're then copied to an actual instance)
event-object[6004].diff (10.6 KB) - added by flesler 12 years ago.
This version makes all the tests pass, adds currentTarget attribute to event objects

Download all attachments as: .zip

Change History (8)

Changed 12 years ago by flesler

Attachment: event-object[5953].diff added

comment:1 Changed 12 years ago by flesler

need: PatchCommit
Status: newassigned

comment:2 Changed 12 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 12 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 12 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 12 years ago by flesler

Resolution: fixed
Status: assignedclosed

Applied at [6005].

comment:4 Changed 12 years ago by flesler

Since [6008], jQuery.Event can be instantiated without the 'new' operator, still optional.

comment:5 Changed 12 years ago by flesler

[6016]

  • 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
Note: See TracTickets for help on using tickets.