Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending load function instead of overriding #235

Open
jln-pl opened this issue Apr 21, 2020 · 4 comments
Open

Extending load function instead of overriding #235

jln-pl opened this issue Apr 21, 2020 · 4 comments

Comments

@jln-pl
Copy link

jln-pl commented Apr 21, 2020

Expected Behavior

I would like to extend load function not to override it, e.g. add dispatching some event, add some logs etc.

Current Behavior

I can provide my own load function which override the default one. So if I write something like this:

lozad('.lozad', {
    load: function(el) {
        console.log('loading element');
    }
});

lazy loading will not work, because my function (with only console.log) will override the default one.

Possible Solution

Export load function from defaultConfig, so I can do this:

// lozad.js
export function load() {
    ...
}

export default function lozad() {
    ...
}
// my-code.js
import lozad, { load } from 'lozad';

lozad('.lozad', {
    load: function(el) {
        console.log('loading element');
        // dispatch my event
        // do something
       load();
    }
});

Context

I want to replace lazysizes with lozad, but I need to dispatch lazybeforeunveil event.

@igorkamyshev
Copy link
Contributor

Hi!

Maybe for backward compatibility and better API, we can just add new eventListener? Something like onBeforeLoad?

@Giovancruz
Copy link

+1

@Giovancruz
Copy link

On doc describes the method 'loaded', maybe it help you:

If you would like to extend the loaded state of elements, you can add the loaded option:

Note: The "data-loaded"="true" attribute is used by lozad to determine if an element has been previously loaded.

lozad('.lozad', {
    loaded: function(el) {
        // Custom implementation on a loaded element
        el.classList.add('loaded');
    }
});

@Dru-S
Copy link

Dru-S commented Feb 24, 2022

Does anyone have found any solution for this, or someone have already open a pull request?

Maybe it's possibile to simply add a beforeLoad() and an afterLoad() functions, before and after the load() one, something like:

beforeLoad: function beforeLoad() {}
load: function load(element) {
  // Default `load()` things
  ...
},
afterLoad: function afterLoad() {}
loaded: function loaded() {}

and call them in the observe and triggerLoad methods, like:

beforeLoad(element);
load(element);
afterLoad(element);
markAsLoaded(element);
loaded(element);

What do you guys think?

Cc @ApoorvSaxena

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants