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.

Moving components around & updating vuex just once.

See original GitHub issue

Hi there,

Thanks so much for this component, massively useful!

I’m trying to use Vue.Draggable to manipulate a nested tree structure. I have used normalizr to normalize my tree, and I can update the components in the store as I drag them around. I made a fiddle here.

My issue is that when moving a component between children, the set method is called twice. It totally makes sense that it’s called twice, but, as I want to update an API I want to do it as one atomic move. Hopefully some code will make my question clear:

My main component has computed methods like this which do the updating:

  computed: {
    children: {
      get() {
        if( this.container.nodes) {
          return this.container.nodes.map((node_id) => this.$store.state.nodes[node_id]);
        } else {
          return [];
        }
      },
      set(value) {
       // this gets called twice when I move a node between containers.
       // which is fair enough, but I want to only post once to my API endpoint, so that the complete
       // move is atomic.
        let newOrder = value.filter(function(n) { return n != undefined }).map(function(v) { return v.id } )
        this.$store.commit('SET_NODES_FOR_CONTAINER', {
          containerID: this.container.id,
          nodes: newOrder
        });
      }
    }
  },

Now, my plan (possibly a bad plan) is that any time there’s a move event I want to persist the move back to an API endpoint. The thing is, I don’t really want to send the entire structure back and forth, just a call with the containerID and and nodes list. The issue is I get two of the set(value) calls, one for the container which had the child moved out of it, and one for where the child got moved into. I want to capture these two events and either do the complete move, or abort.

Is there a way to do this with Vue.Draggable ? Is this a bad approach ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
patrickdaveycommented, Jun 27, 2017

Ah, my mistake, I had a component wrapping the entire tree structure, once I listened on that for the end event it all works perfectly.

Thanks again @David-Desmaisons for a fantastic library!!

0reactions
patrickdaveycommented, Jun 26, 2017

I think the issue I’m having is that I’m dragging the nested draggables around, the components aren’t actually wired up to “know” about the parent-child relationships, so the end event is being emitted but there’s no parent around to actually listen to the event.

Is there a way to wire in an event bus or similar so that I can catch the events? Anyway, I’ll keep on playing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - Vuex store only keeps one component updated, not both
If the properties are not initialized on the state when the store is created, then you will need to use Vue.set. const store...
Read more >
How To Manage State in a Vue.js Application with Vuex
If you change one of the names in your Vuex store, the getter will be updated automatically. Moving on from getters, there are...
Read more >
Adding a new todo form: Vue events, methods, and models
So we now have a form component into which we can enter the title of a new todo item (which will become a...
Read more >
Props | Vue.js
All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down...
Read more >
How To Pass Data Between Components In Vue.js
Unless you're creating your entire Vue app in one component (which ... our data and have any child props using that value will...
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