Dynamic Web Development with Seaside

2.4.3Defining the Code

Now we are ready to define some methods for our component. We will define methods that will be executed on an instance of the WebCounter class. We call them instance methods since they are executed in reaction to a message sent to an instance.

The first method that we will define is the initialize method, which will be invoked when an instance of our component is created by Seaside. Seaside follows normal Smalltalk convention, and will create an instance of the component for us by using the message new, which will create the new instance and then send the message initialize to this new instance.

First raise the context menu in the “method category” pane and select Add category... as shown in Figure 7. Select initialization from the resulting dialog, which will add initialization to the method category pane. Method categories have no effect on the functionality of your components; they are intended to help you organise your work.

Adding a method category

Now ensure that the initialization method category is selected, and then enter the following in the source pane — remember that you do not have to type WebCounter>> in the source code pane:

super initialize.
count := 0

Remember that this definition states that the method initialize is an instance side method since the word class does not appear between WebCounter and >> in the definition.

Once you are done typing the method definition, bring up the context menu for the code pane and select the menu item accept (s), as shown in Figure 8.

At this point Pharo might ask you to enter your full name. This is for the source code version control system to keep track of the author that wrote this code.

Compiling a method

The method signature will also appear in the method pane as shown in Figure 9.

The method has been compiled

Now let’s review what this means. To create a method, we need to define two things, the name of the method and the code to be executed. The first line gives the name of the method we are defining. The next line invokes the superclass initialize method. The final line sets the value of the count instance variable to 0.

To be ready to define Seaside-specific behaviour, now create a new method category called actions. From the method category pane bring up the context menu and select Add category... and type the new category actions. In this new category define two more instance methods to change the counter state as follows.

count := count + 1
count := count - 1

Many programmers like to keep their hands on the keyboard, avoiding the mouse whenever possible. Most of the actions we have described have keyboard shortcuts. Keyboard shortcuts for menu item actions are often indicated in the menu itself. For example the Accept (s) menu item can be activated by pressing the correct keyboard qualifier key together with the s-key. The keyboard qualifier key depends on what platform you’re using, it may be command, control or alt depending on your platform.

Copyright © 21 April 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.