Skip to content

Commit fc68654

Browse files
committed
fix(tree.component): make sure tree can be loaded asynchronously
1 parent e3bc15c commit fc68654

2 files changed

Lines changed: 83 additions & 63 deletions

File tree

demo/app.component.ts

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { NodeEvent, TreeModel, RenamableNode, Ng2TreeSettings } from '../index';
33

44
require('../src/styles.css');
@@ -10,7 +10,7 @@ declare const alertify: any;
1010
template: `
1111
<div class="tree-demo-app">
1212
<div class="tree-container">
13-
<p>Fonts tree</p>
13+
<p class="tree-title">Fonts tree</p>
1414
<tree
1515
[tree]="fonts"
1616
(nodeRemoved)="onNodeRemoved($event)"
@@ -21,7 +21,8 @@ declare const alertify: any;
2121
</tree>
2222
</div>
2323
<div class="tree-container">
24-
<p>Programming languages tree</p>
24+
<p class="tree-title">Programming languages tree</p>
25+
<p class="notice">this tree is loaded asynchronously</p>
2526
<tree
2627
[tree]="pls"
2728
[settings]="settings"
@@ -46,13 +47,18 @@ declare const alertify: any;
4647
vertical-align: top;
4748
width: 500px;
4849
}
49-
.tree-demo-app .tree-container p {
50+
.tree-title {
5051
color: #40a070;
5152
font-size: 2em;
5253
}
54+
.notice {
55+
color: #e91e63;
56+
font-size: 1.2em;
57+
font-style: italic;
58+
}
5359
`]
5460
})
55-
export class AppComponent {
61+
export class AppComponent implements OnInit {
5662
public settings: Ng2TreeSettings = {
5763
rootIsVisible: false
5864
};
@@ -66,85 +72,91 @@ export class AppComponent {
6672
'static': true
6773
},
6874
children: [
69-
{value: 'Antiqua'},
70-
{value: 'DejaVu Serif'},
71-
{value: 'Garamond'},
72-
{value: 'Georgia'},
73-
{value: 'Times New Roman'},
75+
{ value: 'Antiqua' },
76+
{ value: 'DejaVu Serif' },
77+
{ value: 'Garamond' },
78+
{ value: 'Georgia' },
79+
{ value: 'Times New Roman' },
7480
{
7581
value: 'Slab serif',
7682
children: [
77-
{value: 'Candida'},
78-
{value: 'Swift'},
79-
{value: 'Guardian Egyptian'}
83+
{ value: 'Candida' },
84+
{ value: 'Swift' },
85+
{ value: 'Guardian Egyptian' }
8086
]
8187
}
8288
]
8389
},
8490
{
8591
value: 'Sans-serif',
8692
children: [
87-
{value: 'Arial'},
88-
{value: 'Century Gothic'},
89-
{value: 'DejaVu Sans'},
90-
{value: 'Futura'},
91-
{value: 'Geneva'},
92-
{value: 'Liberation Sans'}
93+
{ value: 'Arial' },
94+
{ value: 'Century Gothic' },
95+
{ value: 'DejaVu Sans' },
96+
{ value: 'Futura' },
97+
{ value: 'Geneva' },
98+
{ value: 'Liberation Sans' }
9399
]
94100
},
95101
{
96102
value: 'Monospace',
97103
children: [
98-
{value: 'Input Mono'},
99-
{value: 'Roboto Mono'},
100-
{value: 'Liberation Mono'},
101-
{value: 'Hack'},
102-
{value: 'Consolas'},
103-
{value: 'Menlo'},
104-
{value: 'Source Code Pro'}
104+
{ value: 'Input Mono' },
105+
{ value: 'Roboto Mono' },
106+
{ value: 'Liberation Mono' },
107+
{ value: 'Hack' },
108+
{ value: 'Consolas' },
109+
{ value: 'Menlo' },
110+
{ value: 'Source Code Pro' }
105111
]
106112
}
107113
]
108114
};
109115

110-
public pls: TreeModel = {
111-
value: 'Programming languages by programming paradigm',
112-
children: [
113-
{
114-
value: 'Aspect-oriented programming',
115-
children: [
116-
{value: 'AspectJ'},
117-
{value: 'AspectC++'}
118-
]
119-
},
120-
{
121-
value: 'Object-oriented programming',
116+
public pls: TreeModel;
117+
118+
public ngOnInit(): void {
119+
setTimeout(() => {
120+
this.pls = {
121+
value: 'Programming languages by programming paradigm',
122122
children: [
123123
{
124-
value: {
125-
name: 'Java',
126-
setName(name: string): void {
127-
this.name = name;
124+
value: 'Aspect-oriented programming',
125+
children: [
126+
{ value: 'AspectJ' },
127+
{ value: 'AspectC++' }
128+
]
129+
},
130+
{
131+
value: 'Object-oriented programming',
132+
children: [
133+
{
134+
value: {
135+
name: 'Java',
136+
setName(name: string): void {
137+
this.name = name;
138+
},
139+
toString(): string {
140+
return this.name;
141+
}
142+
} as RenamableNode
128143
},
129-
toString(): string {
130-
return this.name;
131-
}
132-
} as RenamableNode
144+
{ value: 'C++' },
145+
{ value: 'C#' }
146+
]
133147
},
134-
{value: 'C++'},
135-
{value: 'C#'}
136-
]
137-
},
138-
{
139-
value: 'Prototype-based programming',
140-
children: [
141-
{value: 'JavaScript'},
142-
{value: 'CoffeeScript'},
143-
{value: 'TypeScript'}
148+
{
149+
value: 'Prototype-based programming',
150+
children: [
151+
{ value: 'JavaScript' },
152+
{ value: 'CoffeeScript' },
153+
{ value: 'TypeScript' }
154+
]
155+
}
144156
]
145-
}
146-
]
147-
};
157+
};
158+
}, 2000);
159+
}
148160

149161
public onNodeRemoved(e: NodeEvent): void {
150162
AppComponent.logEvent(e, 'Removed');

src/tree.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Input, Component, OnInit, EventEmitter, Output, Inject } from '@angular/core';
1+
import { Input, Component, OnInit, EventEmitter, Output, Inject, OnChanges, SimpleChanges } from '@angular/core';
22
import { TreeService } from './tree.service';
33
import { TreeModel, Ng2TreeSettings } from './tree.types';
44
import { NodeEvent } from './tree.events';
@@ -9,7 +9,9 @@ import { Tree } from './tree';
99
template: `<tree-internal [tree]="tree" [settings]="settings"></tree-internal>`,
1010
providers: [TreeService]
1111
})
12-
export class TreeComponent implements OnInit {
12+
export class TreeComponent implements OnInit, OnChanges {
13+
private static EMPTY_TREE: Tree = new Tree({value: ''});
14+
1315
/* tslint:disable:no-input-rename */
1416
@Input('tree')
1517
public treeModel: TreeModel;
@@ -38,9 +40,15 @@ export class TreeComponent implements OnInit {
3840
public constructor(@Inject(TreeService) private treeService: TreeService) {
3941
}
4042

41-
public ngOnInit(): void {
42-
this.tree = Tree.buildTreeFromModel(this.treeModel);
43+
public ngOnChanges(changes: SimpleChanges): void {
44+
if (!this.treeModel) {
45+
this.tree = TreeComponent.EMPTY_TREE;
46+
} else {
47+
this.tree = Tree.buildTreeFromModel(this.treeModel);
48+
}
49+
}
4350

51+
public ngOnInit(): void {
4452
this.treeService.nodeRemoved$.subscribe((e: NodeEvent) => {
4553
this.nodeRemoved.emit(e);
4654
});

0 commit comments

Comments
 (0)