Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@item-toggle is called twice #67

Open
Tzahile opened this issue Aug 8, 2018 · 15 comments
Open

@item-toggle is called twice #67

Tzahile opened this issue Aug 8, 2018 · 15 comments

Comments

@Tzahile
Copy link

Tzahile commented Aug 8, 2018

I added @item-toggle="ItemToggle"
and created the function:
ItemToggle( ){ ... }

The function ItemToggle is called twice, why?

@ventralnet
Copy link

I am experiencing this as well..

@ventralnet
Copy link

Probably related. If you are using async data and expand a node the load data call is called twice as well.

@MPalarya
Copy link

MPalarya commented Sep 6, 2018

experiencing same issue, made an ugly workaround where i would ignore the second toggle with a timeout.

can anyone suggest a fix?

@ventralnet
Copy link

What I am doing is

import _ from 'underscore'

const debouncedItemToggle = _.debounce((model) => {
    // do what you need to do
}, 250);

export default {
     ....
     ....
     onItemToggle(oriNode, model) {
         debouncedItemToggle(model);
     },

     .....
};

@summerstarlee
Copy link

I think this is a bug. in tree-item.vue; the handleItemToggle methods changed the status of open and carry out the this.onItemToggle method, but you can see, in watch, on model.opened change, the
this.onItemToggle method be carry out again. that is why @item-toggle is called twice

@sburr
Copy link

sburr commented Jan 4, 2019

The underlying issue here is even worse in my case, because it is causing my (expensive) @async function to be called twice every time a folder is expanded for the first time.

Is there a fix or workaround that would work?

Following is a simple example to reproduce the issue:

<template>
  <v-jstree ref="tree" class="tree" v-bind:data="nodes" v-bind:async="loadChildren" />
</template>

<script>
import VJstree from "vue-jstree"

export default {
  name: "bug",
  data: function() {
    return {
      nodes: []
    }
  },
  methods: {
    loadChildren: function(ctx, cb) {
      var id = ctx.data.id
      var done = function() {
        console.log("Children loaded")
        return cb( [{text: "Rabbit Hole", isLeaf: false}] )
      }
      console.log("Simulating long time loading children for node " + id + "...")
      setTimeout(done, 2000)
    }
  },
  components: {
    VJstree
  }
}
</script>

<style>
.tree {
  width: 400px;
  height: 400px;
  border: 1px solid #999;
  float: left;
  overflow: scroll;
}
</style>

which produces the following console output...

bug.vue:22 Simulating long time loading children for node undefined...
bug.vue:19 Children loaded
bug.vue:22 Simulating long time loading children for node 2...
bug.vue:22 Simulating long time loading children for node 2...
bug.vue:19 Children loaded
bug.vue:19 Children loaded
bug.vue:22 Simulating long time loading children for node 6...
bug.vue:22 Simulating long time loading children for node 6...
bug.vue:19 Children loaded
bug.vue:19 Children loaded
bug.vue:22 Simulating long time loading children for node 10...
bug.vue:22 Simulating long time loading children for node 10...
bug.vue:19 Children loaded
bug.vue:19 Children loaded

@ventralnet
Copy link

@sburr see what I posted above as a workaround. You will have to debounce the call to load your children so it only happens once. Dirty, but effective.

@sburr
Copy link

sburr commented Jan 4, 2019

Thank you. I did read that but had missed a nuance in the way that underscore's debounce method works in an effort to avoid yet another dependency.

@ventralnet
Copy link

ventralnet commented Jan 5, 2019 via email

@jupe
Copy link

jupe commented Mar 17, 2020

If you are using async data and expand a node the load data call is called twice as well.

I'm experiences this also. Annoying issue..

@ctonsing
Copy link

What I am doing is

import _ from 'underscore'

     .....
};

Some more details for a quick workaround in tree.vue using TypeScript library ts-debounce:

import { debounce } from 'ts-debounce';
....
const debouncedItemToggle = debounce((proxyForThis, oriNode, oriItem, e) => {
  if (oriNode.model.opened) {
    proxyForThis.handleAsyncLoad(oriNode.model[proxyForThis.childrenFieldName],
      oriNode,
      oriItem,
    );
  }
  proxyForThis.$emit('item-toggle', oriNode, oriItem, e);
}, 250);

export default {
     ....
     ....
    onItemToggle(oriNode, oriItem, e) {
      debouncedItemToggle(this, oriNode, oriItem, e);
    },

     .....

};

@SkyOlredja
Copy link

SkyOlredja commented Aug 8, 2022

What I am doing is

import _ from 'underscore'

const debouncedItemToggle = _.debounce((model) => {
    // do what you need to do
}, 250);

export default {
     ....
     ....
     onItemToggle(oriNode, model) {
         debouncedItemToggle(model);
     },

     .....
};

how to call method or variable in "debouncedItemToggle" sir? can you give an example? I'm new to vue-jstree

@ventralnet
Copy link

What I am doing is

import _ from 'underscore'

const debouncedItemToggle = _.debounce((model) => {
    // do what you need to do
}, 250);

export default {
     ....
     ....
     onItemToggle(oriNode, model) {
         debouncedItemToggle(model);
     },

     .....
};

how to call method or variable in "debouncedItemToggle" sir? can you give an example? I'm new to vue-jstree

You can pass in 'this' to the debouncedItemToggle

@renqWork
Copy link

renqWork commented Aug 15, 2023

I have modified this method,delete this code" ,this.onItemToggle(this,this.model)",and it worked ;I don't know whether there will be other side effects.
handleItemToggle: function(e) {
this.isFolder && (this.model.opened = !this.model.opened)
},

@Tzahile
Copy link
Author

Tzahile commented Aug 27, 2023

@renqWork good job man 🥳
Please open a PR and notify the Author.
After your fix is merged to the main branch and published, close this issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants