Dynamic Web Development with Seaside

2.4.6Automatically Registering a Component

In the future, you may want to automatically register some applications whenever your package is loaded into an image. To do this, you simply need to add the registration expression to the class initialize method of the component. A class initialize method is automatically invoked when the class is loaded from a file. Here is the initialize class method definition.

WebCounter class>>initialize
WAAdmin register: self asApplicationAt: 'webcounter'

The word “class” in the WebCounter class>> first line indicates that this must be added as a class method as described below.

Because this code is in the WebCounter class, we can use the term self in place of the explicit reference to WebCounter that we used in the previous section. In Smalltalk we avoid hardcoding class names whenever possible.

Let’s implement the method. Select the class WebCounter, click on the Class button under the class pane. You are now browsing the class methods of the class WebCounter (and there are none yet). Define a method category as we did previously: click on the third pane and bring up the context menu. From this menu, select the menu item add category, and from the list select or type class initialization. Then in the bottom pane define and accept the method initialize as shown in Figure 12.

Compiling a class method

In the future, we will add configuration parameters to this method, so it is important to be familiar with creating it. Remember that this method is executed automatically only when the class is loaded into memory from some external file/source. So if you had not already executed WAAdmin register: WebCounter asApplicationAt: 'webcounter' Seaside would still not know about your application. To execute the initialize method manually, type WebCounter initialize in a workspace; your application will be registered and you will be able to access it in your web browser.

Automating the configuration of your Seaside application via class-side initialize methods play an important role in building deployable Smalltalk images because of their role when packages are brought into base images, and is a useful technique to bear in mind for future use.

The following Figure 13 shows a trick Smalltalkers often use: it adds the expression to be executed as comment in the method. This way you just have to put your cursor after the first double quote, click once to select the expression and execute it using the Do it (d) menu item or shortcut.

Adding the executable comment

Copyright © 19 March 2024 Stéphane Ducasse, Lukas Renggli, C. David Shaffer, Rick Zaccone
This book is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 license.

This book is published using Seaside, Magritte and the Pier book publishing engine.