Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added ability to update data through an update method #141

Merged
merged 4 commits into from
Dec 18, 2019

Conversation

baryla
Copy link
Contributor

@baryla baryla commented Jul 28, 2019

I have quite frequently ran into a situation where I was using the "data" object on a node for various reasons. I ran into a situations where I wanted to update the X amount of nodes at the same time so I would first have to find them, loop through them and update a specific value and this approach felt cumbersome.

So I thought it would be nice to have a method to update the data object on a given node. The reason why it's just the data object and not other things is because that's the only object that can be safely updated without unintended consequences + we already have other helpers for nodes like select, unselect etc to update the state.

The way you use it is like this:

this.$refs.tree.update('Item 1', node => {
    return { path: '/path/to/node' }
});

The first argument of the update method is exactly the same as the argument in the find method where it's the criteria. So it can be RegExp or just a string to match the text. Second argument is callback which gives you access to each of the nodes that matched the criteria. The return statement in the callback is what is added to the data object of the node. So you could even override the text by simply doing:

this.$refs.tree.update('Item 1', node => {
    return { text: 'Item 2' }
});

@amsik
Copy link
Owner

amsik commented Jul 30, 2019

It is not obvious that this method updates the data property. Would be better to rename as updateData?

@baryla
Copy link
Contributor Author

baryla commented Jul 30, 2019

@amsik Good point. That may be a good idea.
Unless we add the ability to also update the state in the same method and it would be like this:

this.$refs.tree.update('Item 1', node => {
    return {
        data: { text: 'Item 2' },
        state: { selected: true }
    }
});

What do you think? This way, if there was ever more "updatable" properties in the Node object, we could easily allow that and have one method for them all instead of updateData, updateState, updateXYZ etc. The docs would have to state which properties can be updated this way and which can't.

Just to make it even more clear, if you just do:

this.$refs.tree.update('Item 1', node => {
    return {
        data: { text: 'Item 2' }
    }
});

The state of that particular node will not be modified. Only the properties you stated.

@amsik
Copy link
Owner

amsik commented Jul 31, 2019

Please take a look at the next code:

  select (extendList) {
    if (!this.selectable() || this.selected()) {
      return this
    }

    this.tree.select(this, extendList)

    this.state('selected', true)
    this.$emit('selected')

    return this
  }

Code above shows how select method works. Your method will need to take into account the events, selectedNodes, checkedNodes and so on... a lot of logic :) Do you have a solution in this case?

@baryla
Copy link
Contributor Author

baryla commented Aug 2, 2019

@amsik - I'll look into this as soon as I have some spare time :)

@amsik
Copy link
Owner

amsik commented Aug 2, 2019

Thanks for your contributing! I'll do as well :)

@undergroundwires
Copy link

Looking forward to this. I agree that updateData is a better name. It should modify only changed properties, e.g. if expanded is not updated expanded should stay as the same as before.

For now I wrote a workaround where I iterate current model and and call setModel for by only changing of changed properties recursively. I can work on the pull request when I have time to make it as I described if you agree. But do not depend on me and feel free to complete it as you want as I may have time not very soon.

@baryla
Copy link
Contributor Author

baryla commented Nov 10, 2019

Jeeez I totally forgot about this. I'll try to look into this in the next few days but no promises 🤣

@baryla
Copy link
Contributor Author

baryla commented Nov 11, 2019

I have updated the PR. I decided to take your advice and rename the method to updateData. I decided to drop the idea of supporting state because we can easily change it using this method like so to change the selected state:

this.$refs.tree.updateData('Item 1', node => {
    node.select();

    return { text: 'Item 2' };
});

@baryla
Copy link
Contributor Author

baryla commented Dec 2, 2019

Any feedback @amsik?

@amsik amsik merged commit eaa86cd into amsik:master Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants