Table of Contents Previous Section
Synchronizing Attributes in Parent and Child Components
Because WebObjects treats attribute bindings between parent and child components as potentially two-way communication paths, it synchronizes the values of the bound variables at strategic times during the component action request-response loop. This synchronization mechanism has some implications for how you design components. Also, you can disable synchronization for the component action request-response loop as described in "Disabling Component Synchronization".For the sake of illustration, consider a page that displays a value in two different text fields-one provided by the parent component and one by the child (see Figure 35).
Figure 35. Synchronized Components
CHILDCOMPONENT: ChildComponent {When a value is entered in a field and the change submitted, WebObjects will, if needed, synchronize the value in the parent (parentValue) and child (childValue) at each of the three stages of the component action request-response loop:
childValue=parentValue;
};
- Before and after the components receive the takeValuesFromRequest:inContext: message.
- Before and after the components receive the invokeActionForRequest:inContext: message.
- Before and after the components receive the appendToResponse:inContext: message.
Key-value coding always first attempts to set properties through accessor methods, reverting to accessing the instance variables directly only if the required accessor method is missing.
Given that synchronization occurs several times during each cycle of the request-response loop and that key-value coding is used to accomplish this synchronization, how does this affect the design of reusable component? It has these implications:
- You rarely need to implement accessor methods for your component's instance variables.
- If you do provide accessor methods, they should have no unwanted side effects and should be implemented as efficiently as possible since they will be invoked several times in a request-response loop cycle.
- If you bind a component's attribute to a method rather than to an instance variable, you must provide both accessor methods: one to set the value and one to return it. This is only the case if no instance variable exists, and does not apply to actions.
For instance, it's sufficient in the example shown in Figure 35 to simply declare a childValue instance variable in the child component and a parentValue instance variable in the parent. You need to implement accessor methods (such as setChildValue: and childValue) only if the component must do some calculation (say, determine how long the application has been running) before returning the value.
Let's say the parent component in the example shown in Figure 35 doesn't have a discrete parentValue instance variable but instead stores the value in some other way (for example, as an entry in a dictionary object). In that case, the parent component must provide both a parentValue method (to retrieve the value) and a setParentValue: method (to set it). During synchronization, WebObjects expects both methods to be present and will raise an exception if one is missing.
Table of Contents Next Section