Previous | Next
WireHose Developers Guide

Building TagDrillerPage

The "add this to my page" button is bound to WHTagDrillerPage's addTag method. In this step, you'll subclass WHTagDrillerPage and override the addTag method so that it returns a signup page if the current user is a guest.

Because the layout dictionary may specify that pages may be substituted in a particular layout, here we'll use the session helper's nameForPage method to determine the actual page class to return from addTag().

  1. Select the Web Components group in the Files pane.
  2. Choose New File... from the File menu. Scroll down to the WireHose Page template, and click Next.
  3. Name it TagDrillerPage, add it to the Application Server target in the Hello World target, and click Finish.
  4. Edit TagDrillerPage.java so it reads like this:
    public class TagDrillerPage extends WHTagDrillerPage {
       
        public TagDrillerPage(WOContext context) {
            super(context);
        }
       
        public boolean shouldUseAlternateTemplate() {
            return true;
        }
        
        public WOActionResults addTag() {
            if (helper().user().isGuest()) {
                SignupPage nextPage = 
                  (SignupPage)pageWithName(helper().nameForPage("SignupPage"));
                nextPage.setTag(tag());
                return nextPage;
            } else {
                return super.addTag();
            }
        }
    }
  5. Find this entry in the layout dictionary:
    WHTagDrillerPage = {
        ...
        pageName = WHTagDrillerPage;
        ... 

    Change it so it reads:

    WHTagDrillerPage = {
        ...
        pageName = TagDrillerPage; 
        ...

    WireHose will now use TagDrillerPage instead of WHTagDrillerPage.

How this works: In this example, we're subclassing WHTagDrillerPage to modify behavior, but not duplicating any of its HTML or .wod definitions. The page will still look just like a WHTagDrillerPage, though.

The key is the shouldUseAlternateTemplate method. Since it returns true here, TagDrillerPage will ignore its normal HTML and .wod definitions, and instead get them from two methods called templateHTMLString and templateDefinitionString. By default, these methods will return the HTML and .wod from the component's superclass. So TagDrillerPage ends up using WHTagDrillerPage's template.

You can override templateHTMLString and templateDefinitionString to return strings from any source. A very flexible and powerful technique is to store HTML and component definitions in a database.


Previous | Next