Hi David,
This is another great question, because it’s such a common architecture. Your subsequent solution will definitely work, and I tend to use that approach myself for top-level navigation methods. (I usually have functions so I can do something like Module1.nav("Foo")
, so that each form doesn’t need to understand the exact internals of my main Dashboard form.)
For smaller scale things like clicking on line items, there are two other good approaches.
-
[UPDATE: This is actually unnecessary. Anvil components already have a
parent
attribute that refers to the parent component. There is no need to do this manually.]Pass a reference to the parent form into the constructor when you create a ‘sub’ form, and store it as something likeself.parent
. Then you can access attributes on the parent object directly. This works, but gets a bit cumbersome if you have multiple levels of nesting. -
Use the Anvil component event system. This will let you do something like
self.raise_event("x-clicked")
in the child form, and then in the parent form you can dochild_form.set_event_handler("x-clicked", self.child_click)
, assuming you have a method calledchild_click
in the parent. Custom events must start"x-"
, but otherwise behave entirely like normal events. I now realise that this is currently badly documented, so I will make sure that gets fixed. The advantage of this method is that your child forms don’t need to understand the architecture of your whole app, they just have to raise events. This makes subsequent modifications and refactoring much easier.
Sorry for the essay, I hope that helps. Do follow up to let us know which method you chose in the end.
- Ian.