mvysny / karibu-dsl

Kotlin Vaadin extensions and DSL
http://www.vaadinonkotlin.eu
MIT License
123 stars 16 forks source link

recommended override of initContent not possible due to final #52

Closed DALDEI closed 1 year ago

DALDEI commented 1 year ago

The comments in KComposite say " Alternatively you can override [initContent] and call [ui] there.:"" Attempting to do so fails due to initContent being a final fun. Use case is a view which is using the return value of navigate() to supply additional params needed for construction. naviagate(someclass::class.java).ifPresent { it.setNeededParams(myobj) }

view UI creation needs to be deferred until after setNeededParams so cant be done in either plain initiate or in init{} block.
Need to either know by convention or explicitly to be able to late init the UI objects before their first reference initContent looks like the natural place to do this.

Workaround - possibly a lazy call to the "ui" method ? Is initContent called before or after the above ?

mvysny commented 1 year ago

You're right: the kdoc is lying here. I'll make initContent non-final and I'll add the tests for that.

However, the way you transfer parameters is a bit non-standard. Your view should implement HasParameter and transfer the parameter via URL (in path or in query params). But that may not fit your use-case.

The initContent() is called when the view is attached to the parent component; that can happen in any arbitrary time after the constructor is called; it can even be called straight from the navigate() function.

I'd rather create a dummy ui, say a VerticalLayout, then populate the layout later on after the parameters are set.

mvysny commented 1 year ago

Fixed in karibu-dsl 1.2.0. As a workaround, if you need to override initContent(), you can extend from Composite<Component>() instead of from KComposite.