Table of Contents Previous Section

Component awake

A component awake method is invoked at the point in a cycle of the request-response loop just before the receiver begins to participate in request handling. It's common to implement a component awake method that initializes transaction and persistent variables. For example, the Main.wos script in the CyberWind application uses awake to initialize the options transaction variable:

- awake {
    options = @("See surfshop information", "Buy a new sailboard");
    return self;
}

See DocumentRoot/WebObjects/Examples/CyberWind for the complete CyberWind source.

The WOComponentController class---an abstract class that implements basic component behavior---defines the awake method. In WOComponentController, awake's implementation does nothing, but you can subclass WOComponentController and override awake to perform any necessary initialization. It is equally common, however, to implement the awake method in a component script. The WOComponentController subclass WOWebScriptComponentController overrides awake to invoke the awake method defined in the corresponding script file if one exists.

For a given component, the awake method is invoked only once in a cycle of the request-response loop. Furthermore, a component's awake method is invoked only in cycles in which the component is participating. Generally, a component participates in a cycle of the request-response loop if:

As an example of the latter, the following method messages the component associated with the "LoginPanel" page:

- messageCountString
{
    id loginPage = [WOApp pageWithName:"LoginPanel"];
    id userName = [loginPage userName];
    id messagesCount = [messages count];
    id countString;
    
    if (messagesCount == 0)
        countString = @"No messages";
    else if (messagesCount == 1)
        countString = @"1 message";
    else
        countString = [NSString stringWithFormat:@"%@ messages", messageCount];
    
    return [NSString stringWithFormat:@"%@ for %@", countString, userName];
}

If the "LoginPanel" component is not already participating in the request-response loop when messageCountString is invoked, the pageWithName message to WOApp creates the component and sends it an awake message.

A component awake method is invoked at different points in the request-response loop depending on when the component begins participating. Before an application dispatches a message to a component, the application invokes the component's awake method if the component has not received an awake message in the current cycle of the request-response loop. Thus, the awake method is invoked before any other method, and won't be invoked again until the next cycle in which the component participates.

The awake method is the best place to initialize transaction and session variables. The advantage of using awake to perform this type of initialization is that the variables are guaranteed to be initialized before any other methods are invoked.

Table of Contents Next Section