Skip to content

Commit 3d0826a

Browse files
geoGeorgii Rychko
authored andcommitted
fix(TreeController): populate new nodes with ids unless they have them. Closes #145
1 parent b467edb commit 3d0826a

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ You can subscribe to `NodeCreatedEvent` by attaching listener to `(nodeCreated)`
450450
`NodeCreatedEvent` has a `node` property of type `Tree`, which contains a created node and a `controller` property, which will give you access to node's controller.
451451

452452
```typescript
453-
{node: <Tree>{...}, controller: <TreeController>{...}}
453+
{node: <Tree>{...}}
454454
```
455455

456456
#### NodeRenamedEvent
@@ -531,7 +531,11 @@ Relevant for loading children via ngrx (or any redux-inspired library).
531531
```
532532

533533
## :gun: Controller
534-
First of all you should know how to get a controller of a particular node. You can get a controller of a node only if you set an id property of a node. For example, your tree structure should look like:
534+
First of all you should know how to get a controller of a particular node. You can get a controller of a node only if you set an id property of a node.
535+
536+
> TIP: Ids for nodes created via the context menu or using a TreeController instance get populated automatically unless nodes had ids before there were added to the tree
537+
538+
For example, your tree structure should look like:
535539

536540
```typescript
537541
public tree: TreeModel = {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,8 @@
9393
"typescript": "2.4.2",
9494
"webpack": "3.8.1",
9595
"zone.js": "0.8.18"
96+
},
97+
"dependencies": {
98+
"uuid": "3.1.0"
9699
}
97100
}

src/tree.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
import { Observable, Observer } from 'rxjs/Rx';
1515
import { TreeModel, RenamableNode, FoldingType, TreeStatus, TreeModelSettings, ChildrenLoadingFunction } from './tree.types';
1616

17+
import * as uuidv4 from 'uuid/v4';
18+
1719
enum ChildrenLoadingState {
1820
NotStarted,
1921
Loading,
@@ -201,6 +203,8 @@ export class Tree {
201203
tree.markAsNew();
202204
}
203205

206+
tree.id = tree.id || uuidv4();
207+
204208
if (this.childrenShouldBeLoaded() && !(this.childrenAreBeingLoaded() || this.childrenWereLoaded())) {
205209
return null;
206210
}

test/tree.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ describe('Tree', () => {
383383

384384
expect(servantTree.hasChild(child)).toEqual(true);
385385
expect(child.value).toEqual('');
386+
387+
expect(child.id).toBeDefined();
388+
expect(child.id).toEqual(jasmine.any(String));
389+
386390
expect(child.children).toEqual(null);
387391
expect(child.isLeaf()).toEqual(true);
388392
expect(child.isNew()).toEqual(true);

0 commit comments

Comments
 (0)