Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Closed
balupton opened this Issue Apr 16, 2011 · 4 comments

Comments

Projects
None yet
3 participants

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.

Owner

madrobby commented Apr 18, 2011

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

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.

Contributor

andregoncalves commented Apr 21, 2011

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.

Owner

madrobby commented Apr 24, 2011

fixed in #145

@madrobby madrobby closed this Apr 24, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment