09.04.2018 Views

tornadofx-guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Workspaces<br />

workspace.dockInNewScope()<br />

When the CustomerList docks the CustomerEditor later on, it happens in this new scope.<br />

But what about the search field?<br />

We would need to provide a separate scope for the CustomerEditor that should show the<br />

result of the search, but were we would also also need to inject the customer model<br />

containing the selected customer into the new scope. This following code is imagined inside<br />

the action that selects a customer from the search result:<br />

fun editCustomer(customer: Customer) {<br />

// Create a view model for the customer<br />

val model = CustomerModel(customer)<br />

// Create a new scope, but keep the current workspace<br />

val newScope = Scope(workspace)<br />

// Insert the customer model into the new scope<br />

newScope.set(model)<br />

// Find the CustomerEditor in the new scope<br />

val editor = find(newScope)<br />

// Dock the editor<br />

workspace.dock(editor)<br />

}<br />

That's a lot of steps. Fortunately, we can do that as well in a single call:<br />

fun editCustomer(customer: Customer) {<br />

}<br />

workspace.dockInNewScope(CustomerModel(customer))<br />

The dockInNewScope function takes a vararg list of injectable objects to insert into the new<br />

scope before looking up our CustomerEditor and docking it.<br />

Separating scopes this way makes sure we can utilize injected view models without being<br />

afraid of other views stepping on our data. It is a pragmatic approach to an intricate problem.<br />

It also gives you a way of bleeding injectables into new scopes, should your use case<br />

require it.<br />

Custom ViewStack optimizations<br />

239

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!