[Seaside] Making URLs stateful / stateless depending on authentication

Pat Maddox patmaddox at me.com
Wed Oct 6 21:27:14 UTC 2010

I wrote a little blog app, and for visitors to my site I want the blog to use pretty URLs.  When I use the site though, I want to use Seaside callbacks, so I just have to login once and then click around the page.

I have it all working but wanted to see if folks had suggestions for a better way.  Here's my basic structure:

PBApp is my root component.  It has the basic layout, does the updateRoot, and has a child component that points to the main contents of the page.

The other main components are PBBlog - a view of all the blog posts - and PBBlogPost - the model and view for a single blog post.  PBBlogPost renders its title, and that's where the magic happens.  When I'm not logged in, the url should /be-pretty-like-this but when I am logged in, it's Seaside callbacks.  For the callbacks to work, that means telling PBApp (root component) to change the child component it points to.  I wasn't sure of the best way to do that, but going through the Seaside book a bit more I decided to try out the Announcements framework to decouple them.  So my code looks like this:

	super initialize.
	blogRoot := PBBlog new.
	self session announcer on: PBLinkClicked do: [ :it | blog := it target ]

(the following PBComponent is just a WAComponent subclass that my other components inherit from, to share methods)

PBComponentrenderBlogLinkOn: html with: linkText targeting: aComponent
	self session isAuthenticated
		ifTrue: [ html anchor 
			callback: [ self session announcer announce: (PBLinkClicked targeting: aComponent)  ]; 
			with: linkText ]
		ifFalse: [ html anchor url: aComponent blogUrl; with: linkText ]

My PBLinkClicked Announcement subclass just carries the component that was targeted.

So you can see what's going on...when we're not authenticated, create a basic HTML <a> tag with the href pointing to a path generated by the component (I have an initialRequest which parses the URL to see if it matches a blog post).  When we are authenticated, use a Seaside callback to announce that this component's link was clicked, which causes the root component to swap it out for viewing.

I'm just looking for some feedback at this point.  What do you guys think?  Is there a more straightforward way to approach this?


More information about the seaside mailing list