bean icon indicating copy to clipboard operation
bean copied to clipboard

Normalize handler signature

Open ryanve opened this issue 12 years ago • 5 comments

To improve jQuery-compatibility and general consistency in the arguments passed to event handlers via different methods, I think that bean.fire() and .trigger() need modification. Consider:

(function ($) {
    var $html = $('html');

    function handler ()  {
        console.log(arguments);
    }

    $html.on('click', handler); // `handler` receives (eventData) when <html> is clicked
    $html.trigger('click');     // `handler` receives (eventData)

    // When triggered with extra params, bean omits the eventData, meaning
    // the line below sends just ('extra-0', 'extra-1') to `handler`.
    // In jQuery the line below sends (eventData, 'extra-0', 'extra-1')
    $html.trigger('click', ['extra-0', 'extra-1']);

}(ender)); // 2012-11-27 ender build jeesh

The arguments passed to handlers should be as normalized as possible between native / custom / triggers etc. The arguments[0] slot should be reserved for an eventData object. In situations without applicable eventData, it could be passed as null or (better) a simple object like:

{
    isBean: true, 
    isTrigger: true // jQuery includes this on triggers
}

ryanve avatar Nov 27 '12 19:11 ryanve

@ryanve I completely agree with you here, and not just for jQuery compatibility, the trigger arg stuff is a bit of a mess. Also, given the fact that we do a lot of manual handler triggering now we can do some Event object emulation stuff, like jQuery does.

Consider this jQuery, a pattern used a bit in Bootstrap:

var e = $.Event('show')
el.trigger(e)
if (!e.isDefaultPrevented()) {
  // do something
}

Being able to create and retain the event object has a heap of benefits.

Unfortunately the horse has bolted on that and it's a bit difficult to change the API. Perhaps we'll try and solicit some feedback on this to see if anyone is actually using the trigger arguments in Bean.

rvagg avatar Nov 27 '12 21:11 rvagg

@ded @connor @st-luke @iamdustan (or anyone else?) got an opinion on this?

rvagg avatar Nov 27 '12 21:11 rvagg

I'm not using trigger args in bean but normalization is a good thing if it can be done without totally screwing people.

luk- avatar Nov 27 '12 23:11 luk-

I'm also not using trigger arguments in any projects using Bean (although may discover some during the conversion, Rod, like we talked about over email). I'm for the first element in the array being reserved for an eventData object.

connor avatar Nov 28 '12 08:11 connor

I agree with the argument presented here that the first argument should be reserved for the eventData object and the second being optional arguments. It’s clean and provides an expected and consistent way to determine how the event was triggered and how to access your custom arguments.

I haven’t used the trigger arguments because of how it’s applied currently (I tried. I may still have used it, but it definitely caused some confusion at the time)

iamdustan avatar Nov 28 '12 15:11 iamdustan