Skip to content

Commit b879338

Browse files
author
Nako Sung
committed
instantiator now supports lambda and array
1 parent f71b830 commit b879338

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

Plugins/UnrealJS/Content/Scripts/instantiator.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
let Outer = Root.GetEngine ? Root.GetEngine().GetEditorWorld() : GWorld
171171

172172
function instantiate(design, scope) {
173-
if (!design || !design.type) {
173+
if (!design || !design.type) {
174174
throw 'failed to instantiate no design!'
175175
}
176176

@@ -238,10 +238,11 @@
238238

239239
bindings = set_attrs(instance, attrs, scope)
240240

241-
var children = design.children || []
241+
var children = _.compact(design.children) || []
242242

243243
function add_child(child, scope, no_directop) {
244-
var c = instantiate(child, scope);
244+
var c = instantiate(child, scope);
245+
if (!c) return
245246
var slot, child_bindings;
246247
if (!no_directop) {
247248
slot = instance.AddChild(c);
@@ -277,7 +278,7 @@
277278
}
278279
instance.children.splice(instance.children.indexOf(child), 1)
279280
} else {
280-
console.error('couldnot find child')
281+
console.error('couldnot find child', child_instance)
281282
}
282283
if (!no_directop) {
283284
child_instance.RemoveFromParent();
@@ -287,6 +288,29 @@
287288
instance.add_child = add_child;
288289
instance.remove_child = remove_child;
289290

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+
290314
children.forEach(function (child) {
291315
add_child(child, scope);
292316
});

0 commit comments

Comments
 (0)