Bug Tracker

Modify

Ticket #11130 (closed bug: fixed)

Opened 2 years ago

Last modified 2 years ago

jQuery.fn.on: binding map with null selector ignores data

Reported by: zzzzbov Owned by: rwaldron
Priority: low Milestone: 1.7.2
Component: event Version: 1.7.1
Keywords: Cc:
Blocking: Blocked by:

Description

Here's an example that will fail

var map,
    data;
map = {
    'foo': function (e) {
        //e.data will report `null` instead of `"bar"`
        console.log('data', e.data);
    }
};
data = 'bar';
$(window).on(map, null, data).trigger('foo');

The problem exists on line 3674

on: function( types, selector, data, fn, /*INTERNAL*/ one ) {
    var origFn, type;

    // Types can be a map of types/handlers
    if ( typeof types === "object" ) {
        // ( types-Object, selector, data )
        if ( typeof selector !== "string" ) { //3674
            // ( types-Object, data )
            data = selector;
            selector = undefined;
        }

It should probably read:

if ( typeof selector !== "string" && selector !== null ) {

Alternatively the docs need to be updated to address this difference:

 http://api.jquery.com/on/#on-events-map-selector-data

selector A selector string to filter the descendants of the selected elements that will call the handler. If the selector is null or omitted, the handler is always called when it reaches the selected element.

Should be changed to say:

If the selector is an empty string or omitted

Change History

comment:1 Changed 2 years ago by zzzzbov

I've created a jsFiddle for this issue:

 http://jsfiddle.net/7M9cN/

comment:2 Changed 2 years ago by rwaldron

  • Owner set to rwaldron
  • Priority changed from undecided to low
  • Status changed from new to assigned
  • Component changed from unfiled to event
  • Milestone changed from None to 1.7.2

Confirmed. I'll look into this now

comment:4 Changed 2 years ago by Rick Waldron

  • Status changed from assigned to closed
  • Resolution set to fixed

Fix #11130: Don't neglect the data arg when event-map is passed.

Changeset: c0da49ff37484764bd0f33fe40d1a5f83d0f39e8

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.