Views
5 months ago

tornadofx-guide

Scopes JPro will create

Scopes JPro will create a new instance of your App class for each new web user. Also, to access the JPro WebAPI you need to get access to the stage created for each user. In this example we subclass Scope to create a special JProScope that contains the stage that was given to each application instance: class JProScope(val stage: Stage) : Scope() { } val webAPI: WebAPI get() = WebAPI.getWebAPI(stage) The next step is to subclass JProApplication to define our entry point. This app class is in addition to our existing TornadoFX App class, which boots the actual application: class Main : JProApplication() { val app = OurTornadoFXApp() override fun start(primaryStage: Stage) { app.scope = JProScope(primaryStage) app.start(primaryStage) } } override fun stop() { app.stop() super.stop() } Whenever a new user visits our site, the Main class is created, together with a new instance of our actual TornadoFX application. In the start function we assign a new JProScope to the TornadoFX app instance and then call app.start . From there on out, all instances created using inject and find will be in the context of that JPro instance. As usual, you can break out of the JProScope to access JVM level globals by supplying the DefaultScope or any other shared scope to the inject or find functions. We should provide a utility function that makes it easy to access the JPro WebAPI from any Component: val Component.webAPI: WebAPI get() = (scope as JProScope).webAPI The scope property of any Component will be the JProScope so we can cast it and access the webAPI property we defined in our custom scope class. 216

Scopes Testing with Scopes Since Scopes allow you to create separate instances of components that are usually singletons, you can leverage Scopes to test Views and even whole App instances. For example, to generate a new Scope and lookup a View in that scope, you can use the following code: val testScope = Scope() val myView = find(testScope) 217

GUIDE
GUIDE
Guide
Guide
GUIDE
GUIDE
GUIDE
GUIDE
guide
GUIDE
Guide
GUIDE
Guide