[Seaside] Re: Finding the parent component of a rendered component

Ramon Leon ramon.leon at allresnet.com
Tue Apr 24 17:27:57 UTC 2007

> Subject: [Seaside] Re: Finding the parent component of a 
> rendered component
> Ok, maybe someone can help me figure out the logic I want 
> here better than what I have figured out.
> I'm making a blog system.  I have the main application 
> component as BlogView, and each post is rendered as a 
> subcomponent BlogPostView.  Right now, I'm passing the 
> BlogView object into each BlogPostView object so that if a 
> BlogPostView's remove anchor is clicked, it verifies what 
> they want to do, then removes it from the collection I'm 
> using to store the posts, and tells the parent to re-generate 
> it's list of children.
> What I want is a way to remove the need for the BlogPostView 
> to have to know that a parent object even exists.  I want the 
> BlogView to be able to generate a new set of children each 
> time it is displayed, but if I do that right now, then any 
> callbacks the children do are screwed up.  How do I get this 
> parent view to generate a new list each time it is displayed, 
> but not keep the callbacks in the children from working?

That's an interesting question and one that comes up often when writing
components.  There is a general solution to that whole class of problems.  

How do components communicate with other components while maintaining loose
coupling and remaining generally composable?  This problems was solved quite
elegantly by Vassili Bykov and his Announcements framework.

The basic idea is to setup an announcer somewhere global, in the session for

    ^ announcer ifNil: [announcer := Announcer new]

Then sublcass Announcement for any interesting thing that might happen like
removing a child.  

Announcement subclass: #RemoveChild instanceVariableNames: 'child'

Any component interested in this announcement registers its interest when it

    super initialize.
    self session on: SARemoveChild send: #removeChild: to: self

Parent>>removeChild: anEvent 
    self children remove: anEvent child

And any component who wants to fire this event simply announces it by
sending in an instance of that custom announcement object.

    self session announce: (SARemoveChild child: self)

Works great, and depending on where you place the announcer, you could even
have different sessions sending events to each other, or different

Attached is a commented and working demo that uses announcements to solve
your remove child problem.

Ramon Leon

-------------- next part --------------
A non-text attachment was scrubbed...
Name: SeasideAnnouncementDemo.st
Type: application/octet-stream
Size: 4549 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/seaside/attachments/20070424/393f33e9/SeasideAnnouncementDemo-0001.obj

More information about the seaside mailing list