andig
andig

Reputation: 13886

Does Zepto support passing data to event handlers like jQuery?

or rather: can I write code that allows passing data to event handlers that runs on both Zepto and jQuery (e.g. like Zurb's Foundation should)?

Having an event handler that needs external data defined at time of declaration, this data can be passed as part of the event.data in jQuery:

var name = $(this).find("a").attr("href");
// add click function
$(this).click({_name: name}, function(event) {
    $("#"+event.data._name).val($(this).find("a").attr("value"));
    return(false);
});

Similar doesn't seem possible in Zepto. How can the same be achieved and how does code need to look that runs on both Zepto and jQuery for this use case?

Upvotes: 0

Views: 294

Answers (1)

andig
andig

Reputation: 13886

This can be done using Zepto's $.proxy function, see http://zeptojs.com/#changelog. $.proxy is compatible between Zepto and jQuery.

Example:

$.proxy(function(json) {
    ...

    // store
    data[this._channel] = json;
}, {_channel: channel})

Upvotes: 1

Related Questions