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

Change Hook Running On Init #68

Closed
mgabi4488 opened this issue Aug 7, 2021 · 5 comments
Closed

Change Hook Running On Init #68

mgabi4488 opened this issue Aug 7, 2021 · 5 comments

Comments

@mgabi4488
Copy link

mgabi4488 commented Aug 7, 2021

Hi guys,
I have a weird issue the change hook is running on plugin initializing, here is the test i did:

// Initialization
let picker = new CP(element);

// change hook listener
picker.on('change', function(r, g, b, a) {
    // firing immediately 
    console.log(1);
});
@taufik-nurrohman
Copy link
Owner

That’s intentional. The change hook will also run on init to tidy/validate the color value. You can disable this effect by adding a placeholder variable to store the initialization state once.

let init = true;

picker.on('change', function(r, g, b, a) {
    if (init) {
        return;
    }
    console.log([r, g, b, a]);
    init = false; // Update the state here!
});

@mgabi4488
Copy link
Author

Thanks, that's exactly how i solved the issue.

@taufik-nurrohman
Copy link
Owner

taufik-nurrohman commented Aug 9, 2021

FYI, you can also make custom hook inside another hook. In your case, that would be something like this:

function onChangeOnce(r, g, b, a) {
    this.fire('start', [r, g, b, a]);
    this.off('change', onChangeOnce);
}picker.on('change',​ ​onChangeOnce);

Later, you can trigger your initialization function via the start hook:

picker.on('start', function(r, g, b, a) {
    console.log('Once!');
});

@evolross
Copy link

This seems weird. Why would we need to design around a test/validation? It's not logical that changed fires when the picker gets instantiated. Although it works well, your color picker package is very cumbersome to use with lots of strange work-arounds.

@taufik-nurrohman
Copy link
Owner

taufik-nurrohman commented Apr 13, 2022

It's not logical that changed fires when the picker gets instantiated.

@evolross There’s a drag event that you can use to prevent this behaviour if you want 😉

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

3 participants