Last active
September 2, 2019 10:18
-
-
Save alilosoft/d7beccd6aaa4fd419aef74e1a928eb83 to your computer and use it in GitHub Desktop.
ViewModel commit listener implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyModel : ItemViewModel<DomainType>() { | |
// this will not be needed if the above solution is integrated to tornadofx.ViewModel | |
// as the notifyCommitListeners() could be called by ViewModel.commit() method. | |
override fun onCommit() { | |
// TODO: notify listeners only when something really changed | |
notifyCommitListeners() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tornadofx.ext | |
import tornadofx.* | |
private val ViewModel.commitListeners by lazy { mutableListOf<() -> Unit>() } | |
// TODO: add commit listeners to specific properties | |
fun ViewModel.onCommit(op: () -> Unit) { | |
this.commitListeners += op | |
} | |
//TODO: notify only listeners for specific properties | |
fun ViewModel.notifyCommitListeners() { | |
commitListeners.forEach { it.invoke() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment