Skip to content

Commit

Permalink
fix(TreeController): populate new nodes with ids unless they have them.
Browse files Browse the repository at this point in the history
Closes #145
  • Loading branch information
geo authored and Georgii Rychko committed Nov 19, 2017
1 parent b467edb commit 3d0826a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ You can subscribe to `NodeCreatedEvent` by attaching listener to `(nodeCreated)`
`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.

```typescript
{node: <Tree>{...}, controller: <TreeController>{...}}
{node: <Tree>{...}}
```

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

## :gun: Controller
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:
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.

> 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
For example, your tree structure should look like:

```typescript
public tree: TreeModel = {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@
"typescript": "2.4.2",
"webpack": "3.8.1",
"zone.js": "0.8.18"
},
"dependencies": {
"uuid": "3.1.0"
}
}
4 changes: 4 additions & 0 deletions src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
import { Observable, Observer } from 'rxjs/Rx';
import { TreeModel, RenamableNode, FoldingType, TreeStatus, TreeModelSettings, ChildrenLoadingFunction } from './tree.types';

import * as uuidv4 from 'uuid/v4';

enum ChildrenLoadingState {
NotStarted,
Loading,
Expand Down Expand Up @@ -201,6 +203,8 @@ export class Tree {
tree.markAsNew();
}

tree.id = tree.id || uuidv4();

if (this.childrenShouldBeLoaded() && !(this.childrenAreBeingLoaded() || this.childrenWereLoaded())) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions test/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ describe('Tree', () => {

expect(servantTree.hasChild(child)).toEqual(true);
expect(child.value).toEqual('');

expect(child.id).toBeDefined();
expect(child.id).toEqual(jasmine.any(String));

expect(child.children).toEqual(null);
expect(child.isLeaf()).toEqual(true);
expect(child.isNew()).toEqual(true);
Expand Down

0 comments on commit 3d0826a

Please sign in to comment.