Skip to content

Commit

Permalink
changed the function to updateData instead of update
Browse files Browse the repository at this point in the history
  • Loading branch information
baryla committed Nov 11, 2019
1 parent 02dff2f commit 8f8ceef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/hexo/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,41 @@ This method is "syntactic sugar" of `Tree.find(criteria, true)`
Remove Node by criteria.


#### [Tree.updateData(criteria, callback)](#Tree-remove-criteria-multiple)

- **Arguments:**
- `{ Object | String } criteria` (see [find method](#Tree-find-criteria-multiple-false))
- `{ Function } callback`

- **Returns:**
- [Selection](#Selection-API)

- **Usage:**

This method updates the `data` object in a given node. It is an extension of `find` method but with a slight twist.
The first argument is a criteria while the second is a callback which should return a Node.
```javascript
this.$refs.tree.updateData('Node Text', node => ({
description: `ID of this node: ${node.id}`
}));
```
Under the hood, there is also a `forEach` which gives you the ability to do something like this:
```javascript
this.$refs.tree.updateData('Node Text', node => {
if (node.data.someData) {
return {
...node.data,
isBoolean: true
}
}

return {
...node.data,
greeting: 'Hello World'
}
});
```

### Selection API

This array-like object has all array methods (forEach, map and so on) because it inherits `Array` object. This collection has very similar behaviour with jQuery. __All actions apply to all items in the collection.__ I'm going to show one example in more details and other methods have similar logic.
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export default class Node {
}
}

setData (data) {
this.data = Object.assign({}, this.data, data);

this.$emit('data:changed', this.data);

return this.data;
}

state (name, value) {
if (undefined === value) {
return this.states[name]
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,14 @@ export default class Tree {
return new Selection(this, [result[0]])
}

updateData (criteria, callback) {
const nodes = this.find(criteria);

nodes.forEach(node => node.setData(callback(node)));

return nodes;
}

getNodeById (id) {
let targetNode = null

Expand Down
4 changes: 4 additions & 0 deletions src/mixins/TreeMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export default {
return this.tree.expandAll()
},

updateData (criteria, callback) {
return this.tree.updateData(criteria, callback);
},

collapseAll () {
return this.tree.collapseAll()
},
Expand Down

0 comments on commit 8f8ceef

Please sign in to comment.