glide icon indicating copy to clipboard operation
glide copied to clipboard

destroy() fires error after second mount() call

Open sashahohloma opened this issue 6 years ago • 9 comments

Hi! Thanks for very useful plugin! I founded some bug in Event Binder I try to mount Glide on small screens and destroy on large screens. When, Glide was mounted after first destroy(), on second destroy() Glide fire error in console and destroy doesn't work There is fiddle

There is my code

window.addEventListener('resize', () => {
    if( window.innerWidth < 1200 ) {
        glide.mount();
    } else {
        glide.destroy();
    }
});

Also, if I set {type: 'carousel'} Glide creates new clones with every attempt to call destroy()

sashahohloma avatar Jun 21 '19 23:06 sashahohloma

@sashahohloma i don't think this issue exists anymore does it ?

gautamz07 avatar Jul 17 '19 07:07 gautamz07

i have the same issue when trying to disable glidejs on a given breakpoint.

thomascharbit avatar Dec 08 '19 18:12 thomascharbit

Did anyone find a workaround?

gustavo-a avatar Jan 28 '20 21:01 gustavo-a

@sashahohloma i don't think this issue exists anymore does it ?

It seems very similar to this one: #388

SylvainFaure avatar Mar 26 '20 14:03 SylvainFaure

I encountered the same issue when I tried to swap the slides and in doing so I was using destroy() and mount(). What I figured is that when you use the destroy() method, the listener property is removed and is not set when you call mount() after that. So any destroy() call will throw the error.

As a workaround, I created a new Glide object instead of calling mount() on the existing one.

instead of:

// First time I setup the slider
var glide = new Glide(slider, settings);

// ...

glide.destroy();
// ... code to update slides ...
glide.mount();

I did this:

// First time I setup the slider
var glide = new Glide(slider, settings);

// ...
glide.destroy();
// ... code to update slides ...
// Setup the slider again to initialize correctly
glide = new Glide(slider, settings);
glide.mount();

I think mount() is not the opposite of destroy() and when you call destroy once, event listeners are lost. And when we call mount() again, it doesn't re-initializes the even listeners. May be a initialize() function that we can call before mount() could help.

ar5-in avatar Apr 22 '20 21:04 ar5-in

lol neat just hit this issue too.

albanyacademy avatar Sep 04 '20 20:09 albanyacademy

Yeah, same scenario, same issue.

It would be nice to use mount() and destroy() indefinitely as OP mentioned.

+1 for that.

EDIT: Or better yet, a way to disable per breakpoint.

EDIT 2: Workaround: add a control variable:

let glideActive = false;

$(window).on('resize', Drupal.debounce( function () {
    if ($(this).innerWidth() <= 992) {
        if (!glideActive) {
            glide = new Glide(slider, settings);
            glide.mount();
            glideActive = true;
        }
    } else if (glideActive) {
        glide.destroy();
        glideActive = false;
    }
}));

nirodaonline avatar Sep 17 '20 11:09 nirodaonline

@ar5-in thanks, didn't come up with that, you just saved my afternoon 😂

jankohlbach avatar Mar 31 '21 12:03 jankohlbach

why not introduce glide.unmount() function and call it a day?

octipus avatar Oct 18 '23 13:10 octipus