From 06f297d68887c7934e66d2c757abc8bf619df66a Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 28 Oct 2017 23:45:26 +0200 Subject: Databinding components Event handlers refactoring --- .../kotlin/pl/treksoft/kvision/form/CheckInput.kt | 32 +++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt') diff --git a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt index 131793b8..07008ddd 100644 --- a/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt +++ b/src/main/kotlin/pl/treksoft/kvision/form/CheckInput.kt @@ -17,6 +17,16 @@ open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override v init { this.id = id + this.setInternalEventListener { + click = { + val v = getElementJQuery()?.prop("checked") as Boolean? + value = (v == true) + } + change = { + val v = getElementJQuery()?.prop("checked") as Boolean? + value = (v == true) + } + } } @Suppress("LeakingThis") @@ -83,15 +93,17 @@ open class CheckInput(type: CHECKINPUTTYPE = CHECKINPUTTYPE.CHECKBOX, override v } override fun afterInsert(node: VNode) { - this.getElementJQuery()?.on("change", { _, _ -> - val v = getElementJQuery()?.prop("checked") as Boolean? - value = (v == true) - true - }) - this.getElementJQuery()?.on("click", { _, _ -> - val v = getElementJQuery()?.prop("checked") as Boolean? - value = (v == true) - true - }) + refreshCheckedState() + } + + override fun afterPostpatch(node: VNode) { + refreshCheckedState() + } + + private fun refreshCheckedState() { + val v = getElementJQuery()?.prop("checked") as Boolean? + if (this.value != v) { + getElementJQuery()?.prop("checked", this.value) + } } } -- cgit