Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

No support for custom event data. $('body').trigger('blah',customData); #143

Closed
balupton opened this Issue · 4 comments

3 participants

@balupton

jQuery supports custom event data. Zepto should to. This requirement is needed for the upcoming version of History.js

var el = 'body', ev = 'click';
$(el).bind(ev,function(event,customData) {
        console.log(customData.asd === 27);
});
$(el).trigger(ev,{asd: 27});

Currently only the event argument is being passed causing the above to fail.

@madrobby
Owner

Definitely should have support for this. Would you be interested in providing a patch+tests?

@balupton

Sure, although I think it will be a bit of work and I'm unfamiliar with the Zepto coding style.

In my brain, this is what will need to be done. Currently there is:

$.fn.trigger = function(event){
    return this.each(function(){
        var e = document.createEvent('Events');
        this.dispatchEvent(e, e.initEvent(event, true, true));
    });
};

Which I believe dispatchEvent does not support passing extra event data to the event handler - I could be wrong, if so please let me know.

So what would need to be done is that events are bound to the element but also stored into an array. Calling trigger will just throw the handlers stored in the array (with the extra state data). A native trigger (such as clicking a button) will call dispatchEvent (with no extra state data, naturally).

But I could totally be wrong.

@andregoncalves

Hey,

Just added support for custom event data + tests.
I added a proxy function to the event listener to make sure that the callback works with the custom data.
This should work with bind and delegate calls.

@madrobby
Owner

fixed in #145

@madrobby madrobby closed this
@stereobooster stereobooster referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@marioestrada marioestrada referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@marioestrada marioestrada referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@stereobooster stereobooster referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.