Table of Contents
Previous Section
WebScript automates the process of accessing non-local variables, whether they're declared in an application script or in a component script. For a non-local variable myVar, for example, you can set and return its value from the script that declares it, as follows:
[self myVar]; [self setMyVar:newValue];
You don't have to implement these methods to invoke them---WebScript does this work behind the scenes. For example, you may notice that the Visitors Application.wos script doesn't implement visitorNum, setVisitorNum:, or setLastVisitor: methods, yet the Main.wos script invokes them.
In these statements:
[self myVar]; [self setMyVar:newValue];
the myVar and setMyVar: messages are sent to self, which indicates that the variable myVar is declared in the script that's accessing it. Sometimes a component script has to access global or session variables declared in the application script. When you work with global and session variables, remember that they're owned by the application object WOWebScriptApplication. To set or return their values, you send a message to the WOWebScriptApplication object. For example, the Main.wos script in the Visitors example includes these statements:
number = [WOApp visitorNum]; [WOApp setVisitorNum:number]; [WOApp setLastVisitor:[WOApp aName]];
WOApp refers to the application object. The global variable WOApp is short for the following statement:
[WOApplication sharedInstance];
This statement returns the single WOWebScriptApplication object that's accessed by all users of an application.
You can also access a non-local variable declared in one script from another script. This is something you commonly do right before you navigate to a new page, for example:
id anotherPage = [WOApp pageWithName:@"Hello"]; [anotherPage setNameString:newValue];
The current script uses the statement [anotherPage setNameString:newValue]; to set the value of nameString, which is declared in the page entitled "Hello".
This example uses the pageWithName: method, which takes the name of a page as an argument and returns that page. You most commonly use pageWithName: inside a method that returns a new page for display in the browser. Such a method could be associated with a hyperlink or a submit button. For example:
- contactPsychicNetwork
{
id nextPage;
nextPage = [WOApp pageWithName:@"Predictions"];
return nextPage;
}