Table of Contents Previous Section

Component Initialization

It's common in a component's init method to initialize component variables. For example, the Department.wos script in the EmployeeBook example application uses init to initialize the departments component variable:

id departments; 
- init {
    id departmentsPath;
    [super init];
    departmentsPath = [WOApp pathForResourceNamed:@"Departments" ofType:@"array"];
    departments = [NSArray arrayWithContentsOfFile:departmentsPath];
    return self;
}

The WOComponent class---an abstract class that implements basic component behavior---defines the init method for components and implements it to initialize some basic attributes. When a component object must be generated in a scripted application, WebObjects automatically creates an instance of a special subclass of WOComponent and adds to it the code from the component script. When you send init to super in an component script, you are invoking the init method of the superclass of the instance: WOComponent. You can also subclass WOComponent and override init to perform any necessary initialization. It is more common, however, to implement the init method in a component script.

A component's init method is invoked only when the component must be created. This happens at the start of a transaction except when the component is restored from the page cache as a result of the user backtracking or a request component returning itself as the response page. Even then, init is invoked only in cycles in which the component is participating. Generally, a component participates in a cycle of the request-response loop if:

The awake method is immediately invoked in a component after init and after each time the component is restored from the page cache. Just as in init, you can implement a component awake method that initializes component variables. For example, the Main.wos script in the CyberWind application uses awake to initialize the options component variable:

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

You can subclass WOComponent and override awake to perform any necessary initialization, but it is more common to implement the awake method in a component script.

Table of Contents Next Section