Componentand any possible children
Components that may exist in the same context. Within that context, it is easy to pass around the subset of instances from one
find()to locate a
View, you will, by default, get back a singleton instance, meaning that wherever you locate that object in your code, you will get back the same instance. Scopes provide a way to make a
Controllerunique to a smaller subset of instances in your application.
Controller, inherit whatever scope they were looked up in, so you normally don't need to mention the scope after looking up the "root" of your tree of elements.
editfunction creates a new
Scopeand injects a
PersonModelconfigured with the selected user into that scope. Finally, it retrieves a
PersonEditorin the context of the new scope and opens a new window.
findallows for the ability to pass in scopes as a parameter easily between classes, so be sure not to forget this step! TornadoFX gives more insight on the ability for passing scopes in new instances of components:
PersonEditoris initialized, it will look up a
PersonModelvia injection. The default context for
findis always the scope that created the component, so it will look in the
personScopewe just created.
FX.defaultScope. In the above example, the editor might have called out to a
PersonControllerto perform a save operation in a database or via a REST call. This
PersonControlleris most probably stateless, so there is no need to create a separate controller for each edit window. To access the same controller in all editor windows, we supply the scope we want to find the controller in:
PersonControllera true singleton object again, with only a single instance in the whole application.
find, and consequently all objects created in that injection run will belong to the supplied scope.
Scopeand put arbitrary data in there. Each TornadoFX
scopeproperty that gives you access to that scope instance. You can even override it to provide the custom subclass so you don't need to cast it on every occasion:
scopeproperty from your code, it will be of type
PersonScope. It now contains a
PersonModelthat will only be available to this scope:
PersonModel, so we just configure the item for that scope and open the editor. Now the editor can override the type of scope and access the model:
Scopeto create a special JProScope that contains the stage that was given to each application instance:
JProApplicationto define our entry point. This app class is in addition to our existing TornadoFX App class, which boots the actual application:
Mainclass is created, together with a new instance of our actual TornadoFX application.
startfunction we assign a new
JProScopeto the TornadoFX app instance and then call
app.start. From there on out, all instances created using
findwill be in the context of that JPro instance.
JProScopeto access JVM level globals by supplying the
DefaultScopeor any other shared scope to the
scopeproperty of any
Componentwill be the
JProScopeso we can cast it and access the
webAPIproperty we defined in our custom scope class.