Binding Values to Dynamic Elements
In the CyberWind example, all of the dynamic elements are bound to variables and methods from the component that contains them (the Main component). It's common to bind to variables and methods declared directly in the current component; however, you can bind to any value that the component can access.This means, for instance, that you can bind to variables from the application or session object because the WOComponent class declares two instance variables, application and session, which point to the current application and the current session. Look at CyberWind's Footer component in WebObjects Builder. This component displays, among other information, the date and time the CyberWind application was started.This date is stored in the application object, not in the Footer component. The Footer component's .wod file contains this declaration:
UP_SINCE:WOString {value = application.upSince.description};To retrieve a value from this binding, WebObjects uses key-value coding, a standard interface for accessing an object's properties either through methods designed for that purpose or directly through its instance variables. With key-value coding, WebObjects sends the same message (takeValue:forKey:, or takeValue in Java) to any object it is trying to access. Key-value coding first attempts to access properties through accessor methods based on the key's name.
For example, to resolve the binding for the WOString element in the Footer component using key-value coding, WebObjects performs the following steps:
- It resolves the value for the application key by looking for a method named application in the component object.
- It resolves the value for the upSince key by looking for a method named upSince in the application object.
- It resolves the value for the description key by looking for a method named description in the upSince object.
In this case, WOComponent (Component in Java) defines the application method, which returns the WOApplication object (WebApplication in Java).
If the method is not found, it looks for an upSince instance variable. In this case, the upSince instance variable is defined in the application's code file.
Because upSince is a date object, it defines a description method, which prints the object's value as a string.
Note: The Java equivalent of the description method is toString, but you must use the WebScript name for methods and literals in the .wod file even though the application is written in Java.
- You must bind to a variable or method accessible by the current component. (You can also bind to constant values.)
- If you bind to a method, the method must take no arguments. (If you need to bind to a method that takes arguments, you can wrap it inside of a method that doesn't take arguments.)
- You can bind to any key for objects that define keys.
For example, dictionary objects store key-value pairs. Suppose you declare a person dictionary that has the keys name, address, and phone. These keys aren't really instance variables in the dictionary, but because WebObjects accesses values using key-value coding, the following binding works:
myString : WOString { value = person.name };
Even if your entire application is written in Java, you must use the Objective-C names for methods and for literals. For example, you must use YES instead of true, NO instead of false, and description instead of toString.
Table of Contents Next Section