Revert original positions if AJAX request fails
See original GitHub issueRelated to https://github.com/SortableJS/Vue.Draggable/issues/45#issuecomment-313503906
Hey, I would like to know how is it possible to cancel a drop and so revert to original positions, if my AJAX request fails.
I know that I can hook onMoveCallback
and return false
to revert to original positions, but AJAX request works asynchronously, not synchronously…
So I found an example with Sortable
only (https://github.com/RubaXa/Sortable/issues/266) which use onUpdate
event, but I don’t know how to reproduce it with Vue.Draggable
.
Here is my code:
<template>
<div>
<draggable
v-model="items"
:options="options"
@start="onDraggableStart"
@update="onDraggableUpdate"
>
<div v-for="item in items">{{ item.name }}</div>
</draggable>
</div>
</template>
<script>
export default {
data() {
return {
options: {
disabled: false,
}
}
},
methods: {
onDraggableStart() {
// will not work because I don't have access to Sortable instance
this.currentOrder = this.toArray();
},
onDraggableUpdate() {
this.options.disabled = true;
axios
.post(...)
.then(() => ())
.catch((error) => {
// Revert order
this.sort(this.currentOrder); // will not work too
})
.finally(() => this.options.disabled = false);
},
}
}
</script>
Maybe I’m missing something… Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
In jQuery, how to revert a draggable on ajax call failure?
I want the draggable to be reverted to its original position if the ajax call on drop returns a failure. Here is the...
Read more >jQuery | ajax() Method - GeeksforGeeks
It is used to specify whether or not to trigger global AJAX event handles for the request. ifModified: It's default value is false....
Read more >Handle Ajax Requests in ASP.NET Core Razor Pages
Here the request fails, there is no AntiForgeryToken present on the page. There are 2 ways to add the AntiForgeryToken. In ASP.NET Core...
Read more >AJAX programming in Application Express: Are you still using ...
This page describes how to implement AJAX requests within Application Express, ... The dynamic action fires "on change", when the value "is not...
Read more >AJAX error handling with jQuery - Wipfli LLP
By looking at the jQuery documentation for the AJAX convenience methods, like $.post and $.get, we notice that we can attach callback methods...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well… When I was writing my issue, I thought about a dirty solution, but it’s working as expected…
I need to specify a ref to
<draggable>
, and now I can access privateSortable
instance and make my things like that:I think you can close this issue, and put label « question » I guess.
2020 update: In addition to @rolandjlevy’s modifications, I also needed to modify the “event.newIndex” part to “event.moved.newIndex” (and the same for the oldIndex). So the code became: