tornadofx / tornadofx / ViewModel / bind

bind

inline fun <reified PropertyType : Property<T>, reified T : Any, ResultType : PropertyType> bind(autocommit: Boolean = false, forceObjectProperty: Boolean = false, defaultValue: T? = null, noinline propertyProducer: () -> PropertyType?): ResultType

Wrap a JavaFX property and return the ViewModel facade for this property

The value is returned in a lambda so that you can swap source objects and call rebind to change the underlying source object in the mappings.

You can bind a facade towards any kind of property as long as it can be converted to a JavaFX property. TornadoFX provides a way to support most property types via a concise syntax, see below for examples.

class PersonViewModel(var person: Person) : ViewModel() {
    // Bind JavaFX property
    val name = bind { person.nameProperty() }
    // Bind Kotlin var based property
    val name = bind { person.observable(Person::name) }
    // Bind Java POJO getter/setter
    val name = bind { person.observable(Person::getName, Person::setName) }
    // Bind Java POJO by property name (not type safe)
    val name = bind { person.observable("name") }
}