Bug Tracker

Modify

Ticket #3662 (closed feature: fixed)

Opened 4 years ago

Last modified 4 years ago

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

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

Change History

Changed 4 years ago by flesler

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

Updated to new changes. Supports object literals as event objects (they're then copied to an actual instance)

Changed 4 years ago by flesler

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

[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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.