I recently upgraded our project’s jQuery file from 1.4.2 to 1.4.4 and it appears that as of 1.4.3 the way we have been using jQuery.data has stopped working.
We have this code:
var events = $(window).data('events');
if (events.scroll)
if (!events.scroll.include(handler))
$(window).scroll(handler);
the purpose is to prevent this particular handler from being bound multiple times.
In 1.4.2, this works fine. In 1.4.4, events is undefined.
function handler() {
//do something
}
$(document).ready(function(){
$(window).scroll(handler);
$('div#test').scroll(handler);
$(window).data('events') -> undefined
$('div#test').data('events') -> Object
});
What changed with this API? How should I list events for window?
I have changed the first line to this:
var events = $(window).data('__events__').events;
a bit messy-looking, but the ability to wire events to plain objects is compelling.
There was a change in jQuery 1.4.3+ for event types, to avoid object name collisions, for
window(or any other plain object) use the key"__events__"instead, like this:The same
__events__key is used for any objects that don’t have a.nodeTypeproperty (whichwindowdoesn’t, so it’s treated like a plain object here).To be clear that this was a conscious, intentional change, it’s included in the jQuery 1.4.3 release notes: