Recipe 19.9. Sending Data Using Data Binding


Problem

You want to send data to the server when making a service call, but you don't want to have to write much ActionScript.

Solution

Use data binding to send data from components to the service.

Discussion

In Recipe 19.7, you learned how to use data binding to display data that is returned from a connector component call. You can also use data binding to determine what values to send to a service via a connector component. For example, a web service method may require a parameter. You can use data binding to tell Flash to retrieve the value for that parameter from a component.

The majority of data binding is the same regardless of whether you're displaying returned data or sending data from components. Refer to Recipe 19.7 for details on data-binding basics. When you are sending data as parameters via data binding, you'll want to add bindings from component properties to the connector component's params field or fields nested under the params field. There are a few additional considerations, discussed in this recipe.

When you are sending data as parameters, you most frequently won't need to format or encode the data. For example, if you allow the user to select a book from a list of books in a combo box and then send that to a Flash Remoting service as a parameter, you may not need to encode or format that data. Likewise, if you want to send a date parameter from a DateChooser or DateField component, you won't need to encode or format that value. However, there are cases in which your application requires greater sophistication. For example, if the combo box of books consists of elements that have id and title properties and you want to send a string in the format of id, title, you'll need a formatter. Likewise, if you want to allow the user to specify a date as a string in a text input component, you'll have to encode that as a Date object if that's what the service method requires. You can accomplish formatting and encoding via the Schema tab of the Component Inspector panel.

Formatting from the Schema tab works very much like formatting from the Bindings tab, as discussed in Recipe 19.8. From the Schema tab, select the field for which you want to apply the formatting. Then select the correct formatter from the formatter parameter menu, and apply the correct formatter options.

Encoding allows you to convert values before they are sent. For example, if you ask the user to specify a date value in a text input component in a particular format (such as MM/DD/YYYY) and the service method requires a Date object, you'll have to encode the string as a Date object. In such a case, use the Date encoder and specify MM/DD/YYYY for the encoding options.

The distinction between formatting and encoding may not be immediately obvious. Specifically, it may be unclear why both the formatter and encoder drop-down menus have Boolean, Date, and Number (Formatter) options. Think of formatters and encoders as having opposite effects. Where a formatter converts a Boolean, date, or number to a string, the encoder converts from a string to a Boolean, date, or number.




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net