Apple WebObjects 3.5 User Manual

Page 186

Advertising
background image

Chapter 10

The WebScript Language

186

To access a component’s methods, you must store the component in the session
and then access it through the session. For example, suppose you wanted to
rewrite the SelectedCars component of the DodgeDemo so that its database
fetch code was in a compiled object and that object directly set the value of the

message

variable in the SelectedCars component. You’d add the following

statement to the

init

method

SelectedCars.wos

:

/* Store the component in the session. */
[self.session setObject:self forKey:@"SelectedCars"];

and then you can access it in you custom object’s

fetchSelectedCars

method this way:

/* Get the component from the session. */
WOComponent *selectedCarsPage = [[[WOApplication application]

session] objectForKey:@"SelectedCars"];

/* Send it a message. */
[selectedCarsPage setMessage:@"You must supply a name and address"];

To avoid compiler warnings, you should declare the scripted method you want
to invoke in your code. This is because scripted objects don’t declare methods—
their methods are parsed from the script at runtime. If you don’t declare their
methods in your code, the compiler issues a warning that the methods aren’t part
of the receiver’s interface.

Note:

This step isn’t strictly required—your code will still build, you’ll just get

warnings.

For the example above, you’d add the following declaration to your object’s
implementation (

.m

) file:

@interface WOComponent (SelectedCarsComponent)
- (void)setMessage:(NSString *)aMessage;
@end

While it’s certainly straightforward to access a scripted object’s methods from
Objective-C code, you may not want to have that degree of interdependence
between your scripts and your compiled code. You may want to minimize the
interdependence to facilitate reusability.

Advertising