when I define a subscription in a daughter view the subscription is not released.
object MyEvent : FXEvent()
class MainView : View("")
override val root = VBox () {
this += ChildView()
}
}
class ChildView : View("") {
init {
subscribe<MyEvent > {
// this not work, not fire after invoke fire() method from MainView
}
}
Thanks
This happens because you manually instantiate the ChildView. You must never instantiate a UI Component manually as it prevents it from taking part in important life cycle events. Either use inject, find, or add. Simply changing this += ChildView() to add<ChildView>() resolves the issue.
There are a number of other issues with your code, here is a cleaned up sample with best practices.
object MyEvent : FXEvent()
class MainView : View() {
override val root = vbox {
add<ChildView>()
button("Fire").action { fire(MyEvent) }
}
}
class ChildView : View() {
override val root = label("Waiting...") {
subscribe<MyEvent> {
text = "Event was fired at ${LocalDateTime.now()}"
}
}
}
Thanks, that work for me, but how send params to ChildView?
I need pass viewmodel to ChildView and currently do it by constructor.
Read up on Scopes in the documentation. They allow you to do just this without requiring parameter passing.
Thanks Grant :) The other alternative is to change ChildView to a Fragment and pass parameters to find. Also well documented in the guide :)
Most helpful comment
This happens because you manually instantiate the ChildView. You must never instantiate a UI Component manually as it prevents it from taking part in important life cycle events. Either use
inject,find, oradd. Simply changingthis += ChildView()toadd<ChildView>()resolves the issue.There are a number of other issues with your code, here is a cleaned up sample with best practices.