PATH  WebObjects 4.0 Documentation > WebObjects Framework Reference



WOComponent

Inherits From:
WOElement : NSObject

Conforms To: NSObject (NSObject)

Declared in: WebObjects/WOComponent.h


Class Description

WOComponent objects dynamically render web pages (or sections of pages) at run time. They provide custom navigation and other logic for the page, provide a framework for organizing constituent objects (static and dynamic HTML elements and subcomponents), and enable the attribute bindings of dynamic elements.

The WOComponent class has many methods that have the same names as methods of the WOApplication class. However, the scope of the WOComponent methods is limited to a component rather than being application-wide. For example, you can control component-definition caching on a per-component basis using setCachingEnabled: , which has a WOApplication counterpart. When this kind of caching is enabled for a component, the application parses the contents of the component directory the first time the component is requested, creates the component definition, stores this object in memory, and restores it for subsequent requests.

WOComponent objects also respond to awake , sleep , and the three request-handling messages: takeValuesFromRequest:inContext: , invokeActionForRequest:inContext: ,and appendToResponse:inContext: . You can override these methods in your compiled subclasses, and thereby integrate your custom behavior into the request-response loop. (You can also override these methods in component scripts using WebScript.)

[Perhaps rewrite this to talk about Java serialization?] [this constructor doesn't exist]By implementing the NSCoding interface (encodeWithCoder ) and the WOComponent(next.util.Coder) constructor protocol (encodeWithCoder: and initWithCoder: methods), WOComponent objects can serialize their state in an object archive. This capability makes WOComponent objects persistent across sessions when they and their "owning" session (which initiates the archiving) are stored in the page or in cookies. These methods are automatically implemented for scripted components.


Subcomponents

A WOComponent object can represent a dynamic fragment of a Web page as well as an entire page. Such subcomponents, or reusable components, are nested within a parent component representing the page or another subcomponent. Each component keeps track of its parent and subcomponents-when a component receives a request-handling message, such as takeValuesFromRequest:inContext: , it forwards that message to its subcomponents

The WOComponent class also provides a child-parent callback mechanism to allow a child component to communicate with its parent. In the parent's declaration file, bind an arbitrary attribute of the child to an action method of the parent. Then, as the last step in the child's action method, invoke performParentAction: with the argument being the arbitrary attribute, returning the object received back as the response page. See the method description for performParentAction: for details.


Adopted Protocols

NSCoding
- encodeWithCoder:
- initWithCoder:
NSCopying
- copy
- copyWithZone:

Method Types

Someone needs to double check these categories.

Creation
- init
Obtaining attributes
- application
- baseURL
- context
- frameworkName
- hasSession
- name
- pageWithName:
- path
- session
Caching
- isCachingEnabled
- setCachingEnabled:
Managing resources
- templateWithName:
- pathForResourceNamed:ofType:
Handling requests
- appendToResponse:inContext:
- awake
- invokeActionForRequest:inContext:
- sleep
- takeValuesFromRequest:inContext:
Logging
- debugWithFormat:
- logWithFormat:
- logWithFormat:arguments:
- validationFailedWithException:value:keyPath:
Template parsing
+ templateWithHTMLString:declarationString:languages:
Components statistics
- descriptionForResponse:inContext:
Invoking actions
- parent
- performParentAction:
Synchronizing components
- hasBinding:
- setValue:forBinding:
- synchronizesVariablesWithBindings
- valueForBinding:
Other
- generateResponse
[This constructor doesn't exist.]The WOComponent (next.util.Coder) constructor returns a WOComponent object after initializing it from an object archive. You can override this constructor to unarchive and reinitialize the state of instances derived from custom WOComponent subclasses. This constructor, which decodes archived state, must, be implemented in conjunction with the encodeWithCoder interface method (next.util.Coding), which you must implement to encode the archived state. Decoding of objects and other data must follow the same sequence as was used in encoding. Use next.util.Coder methods to do the encoding and decoding. The default implementation of this constructor decodes all subcomponents, thereby causing their implementations of this constructor to be invoked.

See also: Coder class, Coding interface


Class Methods


templateWithHTMLString:declarationString:languages:

+ (WOElement *)templateWithHTMLString: (NSString *)anHTMLString declarationString: (NSString *)aDeclarationString languages: (NSArray*)languages

Programmatically creates the component's template using anHTMLString as the HTML template contents and aDeclarationString as the declarations file contents. Returns (as a WOElement object) the graph of static and dynamic elements build by parsing the HTML and declaration strings. You can then use the returned WOElement as the component's template.

See also: - templateWithName:


Instance Methods


appendToResponse:inContext:

- (void)appendToResponse: (WOResponse *)aResponse inContext: (WOContext *)aContext

Component objects associated with a response receive this message during the last phase of the request-response loop. In the append-to-response phase, the application objects (particularly the response page instance itself) generate the HTML content of the page. WOComponent's default implementation of this method forwards the message to the root WOElement object of the component template. Compiled or scripted subclasses of WOComponent can override this method to replace or supplement the default behavior with custom logic.

See also: - invokeActionForRequest:inContext: , - takeValuesFromRequest: inContext:


application

- (WOApplication *)application

Returns the WOApplication object for the current application.

See also: WOApplication class, - context , - session


awake

- (void)awake

Invoked at the beginning of a WOComponent's involvement in a cycle of the request-response loop, giving the WOComponent an opportunity to initialize its instance variables or perform setup operations. The default implementation does nothing.

See also: - init , - sleep


baseURL

- (NSString *)baseURL

Returns the component URL relative to the server's document root, for example: "/WebObjects/MyApp.woa/Resources/Main.wo"

See also: - name , - path


context

- (WOContext *)context

Returns the WOContext object for the current transaction.

See also: WOContext class, - application , - session


debugWithFormat:

- (void)debugWithFormat: (NSString *)aFormatString,...

Like logWithFormat: , prints a message to the standard error device (stderr), but only prints the message if the WODebuggingEnabled user default option is YES. If WODebuggingEnabled is NO, the debugWithFormat: messages aren't printed. See logWithFormat: for information on the format of aFormatString.

See also: - logWithFormat:arguments:


descriptionForResponse:inContext:

- (NSString *)descriptionForResponse: (WOResponse *)aResponse
inContext: (WOContext *)aContext

Records information about the component if it is the response component in the current request-response loop transaction. The default implementation records the component's name. You might override this method if you want to record more information about the component. For example, you might want to record the values of some instance variables as well as the component name.

This message is sent only to the top-level response component, that is, the one representing the entire page. Components nested inside of that top-level component do not receive this message.

If a CLFF log file is kept for this application, the string returned by this method is recorded in that log file. Thus, you must ensure that the string you return can be analyzed by a CLFF-analysis tool.

See also: WOStatisticsStore class


frameworkName

- (NSString *)frameworkName

If the component is stored in a framework, this method returns the name of that framework. For example, if the component is in the framework NeXT_ROOT/System/Library/Frameworks/WOExtensions.framework , then this method returns the string "WOExtensions".

If the component is not stored in a framework, this method returns nil .

See also: WOResourceManager class


generateResponse

- (WOResponse *)generateResponse

Returns a newly-created WOResponse object. WOComponent's implementation of this method translates the receiving component into a WOResponse object by sending iteself an appendToResponse:inContext: message.

See also: - generateResponse (WOResponse)


hasBinding:

- (BOOL)hasBinding: (NSString *)aBindingName

Returns whether the component has a binding named aBindingName.


hasSession

- (BOOL)hasSession

Returns whether the component is already in a session. For example, in direct actions, sessions are lazily created and you can avoid creating another one unnecessarily by calling hasSession before session .

See also: - session


init

- (id)init

Initializes a WOComponent object. If a WebObjects Builder archive file exists in the component directory, it initializes component variables from this archive. An exception is thrown if the method cannot determine the name of the component or if it cannot initialize the object for any other reason. Override init in compiled subclasses to perform custom initializations; as always, invoke super 's init method as the first thing.

See also: - awake


invokeActionForRequest:inContext:

- (WOElement *)invokeActionForRequest: (WORequest *)aRequest
inContext: (WOContext *)aContext

WOComponent objects associated with a request page receive this message during the middle phase of request handling. In this middle phase, the invokeActionForRequest:inContext: message is propagated through the WOElement objects of the page; the dynamic element on which the user has acted (by, for example, clicking a button) responds by triggering the method in the request component that is bound to the action. WOComponent's default implementation of this method forwards the message to the root WOElement object of the component template.Compiled or scripted subclasses of WOComponent can override this method to replace or supplement the default behavior with custom logic.

See also: - appendToResponse:inContext: , - takeValuesFromRequest:inContext:


isCachingEnabled

- (BOOL)isCachingEnabled

Returns whether component-definition caching is enabled for this component. NO is the default.

See also: - setCachingEnabled:


logWithFormat:

- (void)logWithFormat: (NSString *)aFormat,...

Prints a message to the standard error device (stderr). The message can include formatted variable data using printf -style conversion specifiers, for example:

id i = 500;

id f = 2.045;

[self logWithFormat:@"Amount = %@, Rate = %@, Total = %@", 

i, f, i*f];

Note that in WebScript, all variables are objects, so the only conversion specifier allowed is %@ as shown above. In compiled Objective-C code, all printf conversion specifiers are allowed. The equivalent method in Java is logString .


logWithFormat:arguments:

- (void)logWithFormat: (NSString *)aFormat arguments: (va_list)someArguments

Prints a message to the standard error device (stderr). This method is used by logWithFormat: .


name

- (NSString *)name

Returns the name of the component minus the ".wo" extension; for example "Main" is a typical component name.

See also: - baseURL , - path


pageWithName:

- (WOComponent *)pageWithName: (NSString *)aName

Returns a new page instance (a WOComponent object) identified by aName. If aName is nil , the "Main" component is assumed. If the method cannot create a valid page instance, it raises an exception.

See also: - restorePageForContextID: (WOSession), - savePage: (WOSession)


parent

- (WOComponent *)parent

Returns the parent component of the receiver.


path

- (NSString *)path

Returns the file-system path of the component, which is an absolute path and includes the ".wo" extension; for example "C:\Apple\Library\WOApps\MyApp.woa\Resources\Main.wo" is a typical path.

See also: - baseURL , - name


pathForResourceNamed:ofType:

- (NSString *)pathForResourceNamed: (NSString *)aName ofType: (NSString *)aType

Returns the absolute path to the component resource having the name of aName and an extension of aType. The method searches all localized ".lproj" directories of the component before searching directly under the ".wo" component directory for a non-localized resource of the given name and extension.

This method is provided for backwards compatibility only. For WebObjects 3.5 and above, you should use the WOResourceManager API to retrieve resources. WOResourceManager is not able to retrieve resources stored inside component directories.


performParentAction:

- (id)performParentAction: (NSString *)anActionName

Allows a subcomponent to invoke an action method of its parent component bound to the child component (attribute). Parent and child components are "synchronized" when this method returns: the variables that are bound by a declaration of the child component in the parent component's declaration file have the same value.

An example best illustrates this mechanism. Let's say you have a Palette subcomponent, and this WOComponent is nested in a parent component with a "displaySelection" action method. When the user selects an item in the palette (perhaps a color), you want to invoke "displaySelection" to show the result of the new selection (perhaps a car in the new color). The declaration in the parent's ".wod" file would look like this:

PALETTE: Palette {

   selection = number;

   callBack = "displaySelection";

};

The "callBack" item is an arbitrary attribute of the child component bound in this declaration to the parent component's "displaySelection" method.The performParentAction: method is used to activate this binding. Let's assume the child component has an action method called "click"; the implementation would look like this:

- click {             /* this is the child's action */

	selection = /* some value */;

	/* now invoke the parent's action */

	return [self performParentAction:callBack];

}


session

- (WOSession *)session

Returns the current WOSession object. This method creates a new one if there isn't one.

See also: WOSession class, - application , - context , - hasSession


setCachingEnabled:

- (void)setCachingEnabled: (BOOL)flag

Enables or disables the caching of component definitions for the receiving component. WOComponent definitions contain templates and other common information related to components, and are used to generate instances of those components.When this attribute is set to YES, the application parses the HTML template and the declaration (".wod") file of a component once and then stores the resulting component definition for future requests. By default, this kind of caching is disabled so that you can edit a scripted component without having to relaunch the application every time to check the results.(Note that this does not apply to Java subclasses of WOComponent; in this case, you still have to kill and relaunch the application.)

With WOApplication's method of the same name, you can turn component-definition caching off globally. You can then control caching of individual component definitions using WOComponent's version of this method. Selective caching is an especially valuable technique for very large applications where only the most frequently requested components should be cached.

See also: - isCachingEnabled


setValue:forBinding:

- (void)setValue: aValue forBinding: (NSString *)aBindingName

Sets the value of the binding specified by aBindingName in the parent component to aValue. If the parent doesn't provide aBindingName in its declarations file, this method attempts to set the value in the current component using takeValue:forKey: . If the current component doesn't define this key, this method silently returns.

See also: - synchronizesVariablesWithBindings , - valueForBinding: , "Non-Synchronizing Components" in the Class Description<<Find this cross-ref>>?


sleep

- (void)sleep

Invoked at the conclusion of a request-handling cycle to give component the opportunity for deallocating objects created and initialized in its awake method. The default implementation does nothing.


synchronizesVariablesWithBindings

- (BOOL)synchronizesVariablesWithBindings

Returns whether a nested component pulls all values down from its parent and pushes all values to its parent before and after each phase of the request-response loop. By default, this method returns YES. Override this method to create a non-synchronizing component.

See also: - setValue:forBinding: , - valueForBinding: , "Non-Synchronizing Components" in the Class Description<<Find this cross-ref>>


takeValuesFromRequest:inContext:

- (void)takeValuesFromRequest: (WORequest *)aRequest inContext: (WOContext *)aContext

WOComponent objects associated with a request receive this message during the first phase of the request-response loop. The default WOComponent behavior is to send the message to the root object of the component's template.In this phase, each dynamic element in the template extracts any entered data or changed state (such as a check in a check box) associated with an attribute and assigns the value to the component variable bound to the attribute.Compiled or scripted subclasses of Component can override this method to replace or supplement the default behavior with custom logic.

See also: - appendToResponse:inContext: , - invokeActionForRequest:inContext:


templateWithName:

- (WOElement *)templateWithName: (NSString *)aName

Returns the root object of the graph of static and dynamic HTML elements and subcomponents that is used to graphically render the component identified by aName. This template is constructed from the ".html" and ".wod" file found in the component directory. You identify the template by specifying the component directory, which consists of the component name plus the "wo" extension: for example, "HelloWorld.wo." If the template is not cached, the application will parse the HTML and declaration files of the specified component to create the template.

See also: - setCachingEnabled:


validationFailedWithException:value:keyPath:

- (void)validationFailedWithException: (NSException *)exception
value: (id)value
keyPath: (NSString *)keyPath

Called when an Enterprise Object or formatter failed validation during an assignment. The default implementation ignores the error. Subclassers can override to record the error and possibly return a different page for the current action.


valueForBinding:

- valueForBinding: (NSString *)aBindingName

Gets the value for the specified binding from the parent component. If the parent doesn't provide aBindingName in its delcarations file, this method attempts to get the value from the current component using valueForKey: . If the current component doesn't define this key, this method returns nil. This cascading lookup makes it easy to provide default values for optional bindings.

See also: - setValue:forBinding: , - synchronizesVariablesWithBindings , "Non-Synchronizing Components" in the Class Description. <<Find this cross-ref>>





Copyright © 1998, Apple Computer, Inc. All rights reserved.