Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#5973 closed feature (fixed)

Add A Special Default Hook

Reported by: john Owned by: brandon
Priority: major Milestone: 1.4.2
Component: event Version: 1.4.1
Keywords: Cc:
Blocked by: Blocking:

Description

A way to simulate the default event would be ideal: http://forum.jquery.com/topic/adddefault

Change History (3)

comment:1 Changed 10 years ago by john

Resolution: fixed
Status: newclosed

comment:2 Changed 10 years ago by justinbmeyer

Thanks for letting me know! Yehuda and I talked about this approach for some time and I have a concern that this approach isn't scalable.

I'm assuming that people are going to want to provide the default action as a event API to their plugin. Say you have a tab plugin, the default action for "show" might be to show a specific tab. Users can cancel that event to prevent it.

If I'm understanding the source, there are 2 problems with this approach.

  1. It's very likely that different plugins might trigger the same event. "show" for a a Tab versus a Dialog might behave differently. This only provides 1 default event function for everything that might be clicked. In the browser, default actions are context aware. Clicking a link or a div does different things (at least conceptually). I fear that if I added an "show" default action, I could break every other plugin that might trigger show.
  1. As I think default actions should be easy to make "context" aware, and this context is likely going to be related to a element, a user should be able to define their default actions on the element / event pair. That's why something like:

$(element).bindDefault("trigger", function(){ });

Makes more sense then putting it on jQuery.elements. However, syntax matters much less than the ability to add default events by element / event.

comment:3 Changed 10 years ago by justinbmeyer

The spec (http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-basic) says :

Different implementations will specify their own default actions, if any, associated with each event. The DOM does not attempt to specify these actions.

Seems that default actions are associated with each event. However, the DOM was not designed to have preventDefault as a playground for APIs. The number of default actions is quite limited, and associating a single default action will limit the usefulness of using preventDefault in event drive apis.

And just b/c the w3c has a event standard doesn't mean jQuery has to follow it (trigger would capture and bubble) or improve it (a reliable callback order, stopImmediatePropagation).

Unfortunately, I can't think of a great way of implementing this without making trigger perform slightly worse. The "best" thing I can think of is to have trigger check and accumulate "bound default event handlers" on every element. Even if stopPropagation is called, trigger will keep accumulating bound default handlers as it walks up the parent list.

When it finally gets to the document, it fires each default event.

This way, even if stopPropagation is called, but there is a lower default event, it will be triggered.

This probably won't kill performance, but I understand not wanting to make anything slower in jQuery.

Note: See TracTickets for help on using tickets.