ADOBE FLEX BUILDER 3
Using Adobe Flex Builder 3
204
public function addSearchServiceFaultEventListener(listener:Function):void
• The method on each operation to add the listener to process the result, as the following example shows:
public function addkeywordSearchRequestEventListener(listener:Function):void
The types of events that the service exposes are available via code hints and as metadata on the service. Each event
metadata is commented and exposes the method that dispatches it, as the following example shows:
/**
* Dispatched when a call to the operation KeywordSearchRequest completes successfully
* and returns some data.
* @eventType KeywordSearchRequestResultEvent
*/
[Event(name="KeywordSearchRequest_result", type="KeywordSearchRequestResultEvent")]
When using this approach, the result handling method takes the strongly typed, operation-specific event, as the
following example shows:
public function handleResults(event:KeywordSearchRequestResultEvent):void
{
trace(event.result);//do something with the result
}
Using MXML binding
The generated service class exposes for each operation a specific property, operationName_lastResult, which is
populated each time the result of the operation call is returned. You can bind this property through MXML, and
when the operation returns a value, it automatically populates all bound items. The following example shows a
binding to
operationName_lastResult in an MXML tag:
<mx:Text text={myService.keywordSearchRequest_lastResult} />
Automatically generating Flex Ajax Bridge code
You use the Create Ajax Bridge feature to generate JavaScript code and an HTML wrapper file that let you more easily
use a Flex application from JavaScript in an HTML page. This feature works in conjunction with the Flex Ajax Bridge
JavaScript library, which lets you expose a Flex application to scripting in the web browser. The generated JavaScript
code is very lightweight, as it is intended to expose the functionality that the Flex Ajax Bridge already provides. For
more information about the Flex Ajax Bridge, see “Using the Flex Ajax Bridge” on page 844 in the Adobe Flex 3
Developer Guide. The sample Ajax Bridge application referenced in this topic is available in an importable Flex
Builder project at http://learn.adobe.com/wiki/display/Flex/Download+Projects.
The Create Ajax Bridge feature generates JavaScript proxy code that is specific to the Flex application APIs that you
want to call from JavaScript. You can generate code for any MXML application or ActionScript class in a Flex Builder
project.
For MXML application files, you can generate code for any or all of the following items in the MXML code:
• List of inherited elements, which can expand nonrecursively
• Public properties, including tags with id properties
• Public constants
• Public functions, including classes defined in line
For ActionScript classes, you can generate code for any or all of the following items:
• List of inherited elements
• Public properties; for each property, a get and set method is displayed
ADOBE FLEX BUILDER 3
Using Adobe Flex Builder 3
205
• Public constants
• Public methods
In a directory that you specify, the Create Ajax Bridge feature generates *.js and *.html files that correspond to the
MXML applications and ActionScript classes that you select for generation; it places a copy of the Flex Ajax Bridge
library (fabridge.js) in a subdirectory of the code generation directory. This feature also generates MXML helper files
in the project’s src directory; these files are used to complete the JavaScript code generation.
Generating Ajax Bridge code
1 Right-click a project in the Flex Navigator and select Create Ajax Bridge.
2 In the Create Ajax Bridge dialog box, select the MXML applications and ActionScript classes for which you want
to generate JavaScript code. You can select the top-level checkbox to include the entire object, or you can select
specific members.
3 Specify the directory in which to generate proxy classes.
4 Click OK to generate the code. The following example shows a .js file generated for an application that displays
images:
*
* You should keep your JavaScript code inside this file as light as possible,
* and keep the body of your Ajax application in separate *.js files.
*
* Do make a backup of your changes before regenerating this file. (Ajax Bridge
* display a warning message.)
*
* For help in using this file, refer to the built-in documentation in the Ajax Bridge
application.
*
*/
/**
* Class "DisplayShelfList"
* Fully qualified class name: "DisplayShelfList"
*/
function DisplayShelfList(obj) {
if (arguments.length > 0) {
this.obj = arguments[0];
} else {
this.obj = FABridge["b_DisplayShelfList"].
create("DisplayShelfList");
}
}
// CLASS BODY
// Selected class properties and methods
DisplayShelfList.prototype = {
// Fields form class "DisplayShelfList" (translated to getters/setters):
// Methods form class "DisplayShelfList":
getAngle : function() {
return this.obj.getAngle();
},
setAngle : function(argNumber) {
this.obj.setAngle(argNumber);
},
Get ADOBE® FLEX® BUILDER™ 3: USING ADOBE FLEX BUILDER 3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.