|
170 | 170 | let Outer = Root.GetEngine ? Root.GetEngine().GetEditorWorld() : GWorld
|
171 | 171 |
|
172 | 172 | function instantiate(design, scope) {
|
173 |
| - if (!design || !design.type) { |
| 173 | + if (!design || !design.type) { |
174 | 174 | throw 'failed to instantiate no design!'
|
175 | 175 | }
|
176 | 176 |
|
|
238 | 238 |
|
239 | 239 | bindings = set_attrs(instance, attrs, scope)
|
240 | 240 |
|
241 |
| - var children = design.children || [] |
| 241 | + var children = _.compact(design.children) || [] |
242 | 242 |
|
243 | 243 | function add_child(child, scope, no_directop) {
|
244 |
| - var c = instantiate(child, scope); |
| 244 | + var c = instantiate(child, scope); |
| 245 | + if (!c) return |
245 | 246 | var slot, child_bindings;
|
246 | 247 | if (!no_directop) {
|
247 | 248 | slot = instance.AddChild(c);
|
|
277 | 278 | }
|
278 | 279 | instance.children.splice(instance.children.indexOf(child), 1)
|
279 | 280 | } else {
|
280 |
| - console.error('couldnot find child') |
| 281 | + console.error('couldnot find child', child_instance) |
281 | 282 | }
|
282 | 283 | if (!no_directop) {
|
283 | 284 | child_instance.RemoveFromParent();
|
|
287 | 288 | instance.add_child = add_child;
|
288 | 289 | instance.remove_child = remove_child;
|
289 | 290 |
|
| 291 | + function expand(children) { |
| 292 | + let dirty = false |
| 293 | + children = _.flatten(_.compact(children.map(child => { |
| 294 | + if (typeof child == 'Array') { |
| 295 | + dirty = true |
| 296 | + return child |
| 297 | + } |
| 298 | + else if (typeof child == 'function') { |
| 299 | + dirty = true |
| 300 | + return child(scope) |
| 301 | + } else { |
| 302 | + return child |
| 303 | + } |
| 304 | + }))) |
| 305 | + if (dirty) { |
| 306 | + return expand(children) |
| 307 | + } else { |
| 308 | + return children |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + children = expand(children) |
| 313 | + |
290 | 314 | children.forEach(function (child) {
|
291 | 315 | add_child(child, scope);
|
292 | 316 | });
|
|
0 commit comments