Hi there. Can anyone help me integrate Exposed and TornadoFX properly? I'm still pretty new to Kotli...
w
Hi there. Can anyone help me integrate Exposed and TornadoFX properly? I'm still pretty new to Kotlin/Java in general I have this code:
Copy code
val agents: ObservableList<AgentModel> by lazy {
        transaction(db) {
            addLogger(StdOutSqlLogger)
            AgentEntity.all().map {
                AgentModel().apply {
                    item = it
                }
            }.toObservable()
        }
    }
yet it seems like I can't get the UI to update when the DB changes. Do I need to update the agents list manually?
b
Yes. lazy is
lazy
initialization, which means that your code is executed once on the first call. Alas,
JDBC
does not yet support
notifications
that could help. You should create a separate controller (if you haven't created one), and an empty
ObservableList
. Start an asynchronous task with a repetition of the execution of the query in the database, whether the result of the compare query has changed or not.
ObservableList
- Reacts to additions and deletions.
ObservableMap
- only if the value is not equals. The same goes for
ObservableProperty
w
I just did the housekeeping in the controller and turned on autocommit for AgentModels' properties. Another thing -- how can I return a read-only copy of the same list that would reflect changes in dbController.agents? (though I'm not modifying this list from anywhere else, I want to do this to be safe)
b
you need to modify the existing list. That is, your update in the database should not depend on the ObservableList. You should have a function that checks whether the data has changed or not, and if so, then delete or add new elements. Also, if your model does not have an OBservableProperty, then the model fields will not be updated (you also need to bind to them), you need to delete the old element and add a new one with the changed fields. Also, this crown is not for this channel, this is in the #tornadofx channel
w
yeah, I do exactly this in the controller, and the model has observable properties
b
do you change these properties? And if somewhere how and how often?
w
I set bind(true) to the observable properties in the model, enabling autocommit. I have DBController which maintains the agent list (ObservableList<AgentModel>, the AgentController that does stuff with them. AgentModel is backed by AgentEntity from Exposed of course. So when I write something to model.(property).value it should get added to DB automatically I suppose?
I can show you the code but I keep it in a private github repo
b
No, why do you think so ? JavaFX works with UI, you don't know about Exposed models, unless of course you have created a WrapperProperty
just make an example where the situation will be reproduced