question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Revert original positions if AJAX request fails

See original GitHub issue

Related 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:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Kocalcommented, Feb 7, 2018

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 private Sortable instance and make my things like that:

<template>
  <div>
    <draggable
      ref="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() {
      this.currentOrder = this.$refs.draggable._sortable.toArray();
    },
    onDraggableUpdate() {
      this.options.disabled = true;

      axios
        .post(...)
        .then(() => ())
        .catch((error) => {
           // Revert order
          this.$refs.draggable._sortable.sort(this.draggableCurrentOrder);
        })
        .finally(() => this.options.disabled = false);
    },
  }
}
</script>

I think you can close this issue, and put label « question » I guess.

3reactions
serdarcevhercommented, Apr 2, 2020

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:

onDraggableUpdate(event) {
    ...
    .catch(error => {
       let moved = this.list.splice(event.moved.newIndex, 1);
       this.list.splice(event.moved.oldIndex, 0, moved[0]);
    })
    .finally( ...);
    ...
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found