Open adam-arold opened 5 years ago
The issue here is that only UIComponents are undocked, and hence only subscriptions made in UIComponents will be unregistered. If you move your subscription from the Controller to the View you should observe that the registration is unregistered when the view is undocked.
What we need seems to be some way of saying that a controller injected by a view should be unregistered when the view is undocked. I think this could be done automatically (be the default) for every UIComponent not in the default scope. These are just my preliminary thoughts.
My thinking here is that it should be a Controller
's responsibility to add those listeners. The View
should be just a dumb list of components which delegates to the Controller
. This is how MVC is supposed to work. Am I doing it wrong? I don't want to mix View
code with behavior, that's why I have it in my Controller
. This is the reason why I have onDock
methods in my Controller
s as well.
I absolutely agree with you :)
There is this thing which puzzles me: it would make sense to have onDock
in the Controller
instead of the View
because the Controller
is supposed to tamper with the View
and not the other way around, but in that case the right name should not be onDock
, because a Controller
is an abstract thing, it is not docked. What do you think? How can I have this inversion of control with TornadoFX?
The name Controller is a bit misleading in TornadoFX, because it implies that it's the view controller, but it's not. A controller is more like a service. The View is infact the ViewController in TornadoFX. In any case, we need to have a mechanism to be able to unsubscribe automatically from controllers, so I'll look into this ASAP and get this fixed :)
Do you have a best practices document somewhere? What do you mean by ViewController
? Having both the View
and the Controller
code in one place would quickly lead to convoluted code I think.
The View class should contain the code that builds your UI as well as the code that reacts to events from the UI. Business logic should be placed in the controller. If you use ViewModels you can opt to put view related logic there as well.
I'm using a
Workspace
anddockInNewScope
to navigate between parent and child views. My problem is that if I subscribe to an event in a child view / controller it will get fired even if I navigate away from that view and It becomes effectively unreachable (meaning that it is not reachable from code).Here is the code to reproduce the problem:
Now if I follow this sequence:
this is what will get printed:
So this means that only one
ChildView
gets docked when I go back but the subscription of the oldView
which is unreachable is still active. There is a workaround for this if I do this in theChildController
:but this still seems like a memory leak to me. What am I doing wrong?