Listening to events not working after destroy and remount.
If you destroy using .destroy() or calling destroy: true within a breakpoint event listeners don't work anymore. The below code will not console.log after you visit the breakpoint below 767px and then resize back above 767px
var splide = new Splide( '#home_links', {
perMove: 4,
perPage: 4,
pagination : false,
breakpoints: {
'1199': {
perPage: 3,
perMove: 3
},
'990': {
perPage: 2,
perMove: 2
},
'767': {
perPage: 1,
perMove: 1,
destroy: true
},
}
}).mount();
splide.on('active moved updated', function() {
console.log('now');
});
I think you should use this:
https://splidejs.com/apis#state
splide.on( 'destroy', function() {
// do something
console.log("Destroyed");
} );
But it's also working just one time. After upsizing and following downsizing the splide.on-function will not log again.
Also the resize function will only work until splide was destroyed:
splide.on( 'resize', function() {
if ( ! splide.State.is( splide.STATES.DESTROYED ) ) {
// do something.
console.log("NOT DESTROYED");
} else {
console.log("DESTROYED");
}
console.log("Resized:");
} );
Basically I'm looking for a way to disable and revert to a list instead of slide on a specific breakpoint without loosing event listeners and destroying completely.
I've currently done it using css but would prefer to be able to do it as per the code above.
CSS for reference.
/** Disable on specific breakpoint **/
@media (min-width:767px) and (max-width:990px){
#home_links .splide__arrows, #home_links .splide__counter, #home_links .splide__pagination {
display: none;
}
#home_links .splide__list {
display: block;
transform: none !important;
}
#home_links .splide__slide {
float: left;
}
#home_links .splide__slide--clone {
display: none;
}
}
I had the same problem and I solved it with MediaQueryList API 🙃
Just watch for a media change and then call mount() or destroy() methods.
Close the issue due to the major version update. Feel free to create a new issue or open a new discussion.
destroy removes all handlers because it's "destroy". To keep listening to events after remount, the best way is to create an extension.
https://splidejs.com/guides/extension/
var splide = new Splide( ... );
var MyExtension = function ( Splide ) {
function mount() {
Splide.on( 'active moved updated', function () {
console.log( 'Hey!' );
} );
}
return {
mount,
}
}
splide.mount( { MyExtension: MyExtension } );
The mount function inside the extension is called when Splide mounts and remounts the slider.