Skip to content

Instantly share code, notes, and snippets.

@alilosoft
Last active September 2, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alilosoft/d7beccd6aaa4fd419aef74e1a928eb83 to your computer and use it in GitHub Desktop.
Save alilosoft/d7beccd6aaa4fd419aef74e1a928eb83 to your computer and use it in GitHub Desktop.
ViewModel commit listener implementation
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()
}
}
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