Tornadofx - How to pass a Fragment parameter to each instance

I am new to javafx, kotlin and obviously tornadofx.
Issue:
How to pass parameters to the Fragment on each instance?

Suppose I have a table view as my fragment. Now this fragment is used in several places, but with different data sets.

eg. Adding a snippet to:

class SomeView : View() {
... 
root += SomeViewFragment::class
}

class SomeAnotherView : View() {
... 
root += SomeViewFragment::class
}

Fragment Declaration:

class SomeViewFragment : Fragment() {
...
    tableview(someDataSetFromRestApiCall) {
    ...
    }
}

How can I pass different someDataSetFromRestApiCall from SomeView and SomeAnotherView?

+4
source share
1 answer

. TableView TableView . . SomeItem:

class SomeItem(name: String) {
    val nameProperty = SimpleStringProperty(name)
    var name by nameProperty
}

SomeViewFragment item, TableView:

class SomeViewFragment : Fragment() {
    val items = FXCollections.observableArrayList<SomeItem>()

    override val root = tableview(items) {
        column("Name", SomeItem::nameProperty)
    }
}

, :

class SomeView : View() {
    override val root = stackpane {
        this += find<SomeViewFragment>().apply {
            items.setAll(SomeItem("Item A"), SomeItem("Item B"))
        }
    }
}

SomeOtherView, :

class SomeOtherView : View() {
    override val root = stackpane {
        this += find<SomeViewFragment>().apply {
            items.setAll(SomeItem("Item B"), SomeItem("Item C"))
        }
    }
}

, . , . :

1, . , :

class ItemsModel(val items: ObservableList<SomeItem>) : ViewModel()

ItemsModel :

class SomeViewFragment : Fragment() {
    val model: ItemsModel by inject()

    override val root = tableview(model.items) {
        column("Name", SomeItem::nameProperty)
    }
}

, :

class SomeView : View() {

    override val root = stackpane {
        // Create the model and fill it with data
        val model= ItemsModel(listOf(SomeItem("Item A"), SomeItem("Item B")).observable())

        // Define a new scope and put the model into the scope
        val fragmentScope = Scope()
        setInScope(model, fragmentScope)

        // Add the fragment for our created scope
        this += find<SomeViewFragment>(fragmentScope)
    }
}

, setInScope, , TornadoFX 1.5.9. :

FX.getComponents(fragmentScope).put(ItemsModel::class, model)

- . ItemsScope:

class ItemsScope(val items: ObservableList<SomeItem>) : Scope()

SomeItemScope, :

class SomeViewFragment : Fragment() {
    override val scope = super.scope as ItemsScope

    override val root = tableview(scope.items) {
        column("Name", SomeItem::nameProperty)
    }
}

View , :

class SomeView : View() {

    override val root = stackpane {
        // Create the scope and fill it with data
        val itemsScope= ItemsScope(listOf(SomeItem("Item A"), SomeItem("Item B")).observable())

        // Add the fragment for our created scope
        this += find<SomeViewFragment>(itemsScope)
    }
}

EDIT. find inject. TornadoFX 1.5.9 :

class SomeView : View() {
    override val root = stackpane {
        val params = "items" to listOf(SomeItem("Item A"), SomeItem("Item B")).observable()
        this += find<SomeViewFragment>(params)
    }
}

SomeViewFragment :

class SomeViewFragment : Fragment() {
    val items: ObservableList<SomeItem> by param()

    override val root = tableview(items) {
        column("Name", SomeItem::nameProperty)
    }
}

, , .

EventBus, TornadoFX 1.5.9. EventBus , .

Scopes, EventBus ViewModel :

Scopes

EventBus

ViewModel Validation

+7

Source: https://habr.com/ru/post/1664038/


All Articles