Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery eats my data in custom event for certain property names

Tags:

jquery

When doing this:

$(document.body).on(
  'myevent',
  function (ev) {
    console.log(ev.data, ev.result);
  }
);

$(document.body).trigger(
  $.Event(
    'myevent',
    {
      data: 'foo',
      result: 'bar'    
    }
  )
);

The console returns [null, undefined]. It this something expected? I found it troubling since there is no original event to copy the properties from, and I am running out of property names to send out server response.

ref: http://api.jquery.com/trigger/

ref: http://api.jquery.com/category/events/event-object/

Note of the documents describe this behavior. If some one knows why and explain from the jQuery source code it would be great; we could file a bug if it's something unexpected.

like image 604
timdream Avatar asked Dec 28 '22 08:12

timdream


1 Answers

The ev.data property is the data passed in the .on() call, and there is nothing passed so it returns null. The data property you added in your call to $.Event is overwritten by the null. Don't use any property names mentioned here for your custom data name: http://api.jquery.com/category/events/event-object/

like image 114
Dave Methvin Avatar answered May 25 '23 08:05

Dave Methvin