TornadoFX it's also a CDI framework

During a nice talk in the tornadoFX slack channel one of the members noticed that his Fragments were being added to the view he was working on, but they did not receive any event(like onDock). The reason was that he manually instantiated the fragments:

this += MyFragment(0.0, 0.0, it)

The MyFragment gets instantiated manually, this is the same as in java new MyFragment(0.0, 0.0, 0.0, it) Because of this the init function of the Fragment (or the overridden init in MyFragment) was never called and the fragment never received the onDock event which means it will never add itself. Normally a component like a view or a fragment gets added to the scenegraph like this:

this += find(MyFragment::class)

You can see that the arguments that were given to the MyFragment are not present. TornadoFX has a construct for that:

this += find<MyFragment>("firstArg" to 0.0, "SecondArg" to 0.0)

Then in the myFragment itself you can pick up the arguments like this:

...
val firstArg : Double by param()
val secondArg : Double by param()

This works quite well but it’s quite verbose and feels a bit like boilerplate code. So if possible use a controller or a viewModel.

Allthough very cleverly hidden TornadoFX is in fact a CDI framework. Instantiating a Controller, viewModel, View or Fragment using the find or by inject pattern will tell TornadoFX to either create the requested instance or (except for Fragments) find the one already instantiated.

Yes all Views, ItemModels and Controllers are Singletons

This means that once a itemModel for example has been instantiated, the data in it is available anywhere it gets injected.

comments powered by Disqus
comments powered by Disqus