DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”— Understanding Attached Component Instances in Uniface 10.4

⚠️ 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 the newinstance 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:

  1. All 7 tab forms are asked: "Can you close?" πŸ€”
  2. Each tab can check if it has unsaved data
  3. If one tab says "Wait! The user has unsaved changes!" ⚠️
  4. The entire dialog stays open
  5. 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
Enter fullscreen mode Exit fullscreen mode

🎯 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 or accept 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)