β οΈ Note: This blog post was created with the assistance of AI.
π What Are Attached Component Instances?
In Uniface 10.4, component instances become "attached" to their parent component when you create them in one of two ways:
- Using the
/attached
switch with thenewinstance
command - Setting the "Attach to Parent" widget property to False
Think of attached instances like children that are connected to their parent. When the parent wants to close, all children must agree first! π¨βπ©βπ§βπ¦
πͺ The Two-Phase Close Process
When a parent component wants to exit, attached instances follow a smart two-phase closing process:
Phase 1: Agreement Phase β
All attached instances must agree to close. Each instance checks if it's ready by using the quit
or accept
trigger. The instance returns its answer in the $status
variable:
- 0 means "Yes, I agree to close"
- -1 (or any negative number) means "No, don't close me yet!"
If even one instance says no, the entire close operation is canceled! Nobody gets closed. π
Phase 2: Deletion Phase ποΈ
Only after all instances agree, they are deleted one by one. During this phase, the cleanup
operation runs for each instance (if defined).
π‘ Real-World Example: Tab Widgets
The most common use case for attached instances is with tab widgets. Imagine you have a settings dialog with 7 tabs:
- One parent form contains the tab widget
- 7 attached tab forms (one for each tab)
When you click "OK" or "Cancel" to close the dialog:
- All 7 tab forms are asked: "Can you close?" π€
- Each tab can check if it has unsaved data
- If one tab says "Wait! The user has unsaved changes!" β οΈ
- The entire dialog stays open
- If all tabs agree, they all close cleanly β¨
π§ Simple Code Example
; Create an attached instance
activate "MyChildForm".newinstance(/attached)
; In the quit trigger of MyChildForm
trigger quit
if (someDataHasChanged)
; Ask user to save
if (askUserToSave() == "no")
$status = -1 ; Don't close!
return
endif
endif
$status = 0 ; OK to close
end; quit
π― Key Takeaways
- Attached instances are "children" of a parent component
- All children must agree before closing
- Perfect for tabbed interfaces where data validation is important
- Use the
quit
oraccept
trigger to control closing behavior
π Why This Matters
The two-phase close mechanism prevents data loss! Your users won't accidentally close a window with unsaved changes. It's like having a safety net that catches mistakes before they happen. π‘οΈ
This feature makes your Uniface applications more robust and user-friendly. Users feel more confident when they know their data is protected.
Happy coding! π
Top comments (0)