Mikael Ståldal
07/02/2023, 12:58 PMdave
07/02/2023, 1:06 PMMikael Ståldal
07/03/2023, 5:18 PMdave
07/04/2023, 8:13 AMMikael Ståldal
07/04/2023, 8:14 AMinterface Person {
val firstName: String
val lastName: String
val email: String
}
data class PersonData(override val firstName: String, override val lastName: String, override val email: String) : Person
data class ViewPerson(val personData: PersonData) : HtmlViewModel(), Person by personData
data class EditPerson(val personData: PersonData) : HtmlViewModel(), Person by personData
interface ViewModel {
fun template(): String = javaClass.name.replace('.', '/')
fun model(): Any? = this
}
data class ViewWithModel(val template: String, val model: Any? = null) : ViewModel {
override fun template() = template
override fun model() = model
}
and then change the uses of ViewModel
in all template implementations to use this.model()
instead of this
.
Then I can do like this:
"/" bind Method.GET to {
Response(OK).with(htmlLens of ViewWithModel("index"))
},
"/person/view" bind Method.GET to {
Response(OK).with(htmlLens of ViewWithModel("ViewPerson", person))
},
"/person/edit" bind Method.GET to {
Response(OK).with(htmlLens of ViewWithModel("EditPerson", person))
},
ViewModel
as before, and mix the two.)