I'm using a third-party lazyload plugin: http://www.appelsiini.net/projects/lazyload
In its source, it has the following:
if (0 === settings.event.indexOf("scroll")) {
$container.on(settings.event, function (event) {
return update();
});
}
This applies a 'scroll' event to the parent element. There's no logic in the plugin for unbinding the event. This sucks because sometimes I will add 100s lazy elements and then remove and re-add others without replacing the container. This causes tons of events to stack up!
I'm considering modifying the plugin to read something like:
if (0 === settings.event.indexOf("scroll")) {
// .on('scroll.item_12')
$container.on(settings.event + settings.namespace, function (event) {
return update();
});
// http://stackoverflow.com/questions/2200494/jquery-trigger-event-when-an-element-is-removed-from-the-dom
$(this).on('remove', function() {
$container.off(settings.event + settings.namespace);
});
}
Is this the correct way of handling such a scenario? When working with BackboneJS I have access to: http://backbonejs.org/#Events-listenTo, but I'm not certain that a similiar function exists in just jQuery/Javascript