You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to cancel or delay the swappable swap event?
I'd like to implement swapping, doing an ajax call, depending on the result, success -> continue with swapped, failure, revert the swapping or rather not let it continue. For now event.cancel(); only seems to work with a synchronous call.
functiondelay(t,val){returnnewPromise(resolve=>setTimeout(resolve,t,val));}constcontainers=document.querySelectorAll('#container');if(containers.length===0){returnfalse;}constswappable=newDraggable.Swappable(containers,{draggable: '.Block--isDraggable',mirror: {constrainDimensions: true,},plugins: [Draggable.Plugins.ResizeMirror],});swappable.on('swappable:swap',async(e)=>{awaitdelay(1000);e.cancel();// Does not work anymoreconsole.log('Swap');});
The text was updated successfully, but these errors were encountered:
To achieve asynchronous event handling with the swappable:swap event in the Draggable library, you need to handle the cancellation or continuation of the swap in a way that integrates with your asynchronous logic. The e.cancel() method only works synchronously, so you need to find a way to defer the decision to cancel the event until your asynchronous code completes.
Here's how you can approach this problem by using a combination of requestAnimationFrame and a custom flag to handle the swap after the asynchronous operation completes:
function delay(t, val) {
return new Promise(resolve => setTimeout(resolve, t, val));
}
// Perform your asynchronous operation
const result = await delay(1000);
// Depending on the result, decide whether to cancel the swap or not
if (/* condition to allow swap, e.g., result is successful */) {
shouldCancelSwap = false;
} else {
shouldCancelSwap = true;
}
// Use requestAnimationFrame to wait for the next frame before continuing
requestAnimationFrame(() => {
if (!shouldCancelSwap) {
// Manually trigger the swap event if the condition is met
swappable.trigger('swappable:swap', e);
}
});
});
// Optional: Handle the end of the swappable event to revert if necessary
swappable.on('swappable:end', (e) => {
if (shouldCancel
Is it possible to cancel or delay the swappable swap event?
I'd like to implement swapping, doing an ajax call, depending on the result, success -> continue with swapped, failure, revert the swapping or rather not let it continue. For now event.cancel(); only seems to work with a synchronous call.
The text was updated successfully, but these errors were encountered: