Vous êtes sur la page 1sur 5

Flash and Ajax

“Ajax is likely to become a mainstream tool used by web developers as an alternative


to other RIA technologies such as Flash and Microsoft Windows Presentation
Foundation “Everywhere” (WPF/E), but it also provides an excellent solution when
integrated with other RIA technologies, in particular Flash.“
MAX 2006 - Burton Group, January 2006
Beyond Boundaries
“AJAX and Flash are like peanut butter and jelly; each is good in its own right, but they are best
when put together”
Jason Williams
- Richard Monson-Haefel, Burton Group, March 2006
Integrating Flex Apps with
Browsers and AJAX
Adobe Systems, Inc.

1 2
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

Why Flash? Demo

ƒ Additional functionality, providing richer experiences, that can reach everyone.

ƒ Audio and Video


ƒ Drawing and Animation
ƒ Local Storage
> PLACE HOLDER FOR IMAGES OF DEMO <
ƒ Data integration
ƒ Backwards and Forwards
Compatibility at the SWF
level

3 4
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

Experience matters FSCommand and SetVariable (“the hard way”)

ƒ fscommand() provides a primitive means of communication with the Flash


Player host, using a single function with a “command” and “arguments” pattern.

Step 1: setup DoFScommand() in JavaScript


<html>
...
<script>
function mySwf_DoFScommand(command, args)
{
if (command == “displayAlert”)
{
displayAlert(args);
}
else (command == “updateDisplay”)
...
}
</script>
</html>

5 6
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

1
FSCommand and SetVariable (“the hard way”) FSCommand and SetVariable (“the hard way”)

ƒ To call the JavaScript function xxx_DoFScommand(…) use fscommand() within ƒ SetVariable() provides a primitive means of communication with the Flash
the ActionScript. Player using JavaScript.

Step 2: call JavaScript using fscommand() from AS


Step 3: Call into ActionScript using SetVariable()
package
{ <html>
import flash.display.Sprite; ...
… <script>
public class FSCommandExample extends Sprite function drawCircle(dia, x, y, color)
{ {
function FSCommandExample() mySwf.SetVariable(“circleDia”, dia);
{ mySwf.SetVariable(“circleX”, x);
super(); mySwf.SetVariable(“circleY”, y);
fscommand(“displayAlert", “Hello from JavaScript"); mySwf.SetVariable(“circleColor”, color);
} mySwf.SetVariable(“drawCircle”, true);
… }
} </script>
} </html>

7 8
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

FSCommand and SetVariable (“the hard way”) ExternalInterface (“the right way”)

ƒ SetVariable will call a setter function on the root of the current swf ƒ ExternalInterface provides a programming interface that allows straight forward
two-way communication between ActionScript and the Flash Player host
environment.
Step 4: Setup AS code to handle SetVariable calls
package Step 1: Setup AS code that will be invoked from JavaScript
{
import flash.display.Sprite; package
{
… import flash.display.Sprite;
public class FSCommandExample extends Sprite
{ public class ExternalInterfaceExample extends Sprite
… {
public function set circleDia(value:int):void …
{ public function drawCircle(dia:int, x:int, y:int, color:int):void
{
_circleDia = value; var circle:Shape = new Shape();
} child.graphics.beginFill(color);
… child.graphics.drawCircle(dia, dia, dia);
} child.graphics.endFill();
} addChild(circle);
}

9 10
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

ExternalInterface (“the right way”) ExternalInterface (“the right way”)

ƒ ActionScript can call JavaScript directly and synchronously. ƒ ExternalInterface is available on the following platforms:
ƒ Get return values, pass any data type arguments, including strongly typed objects
Browser Operating System
Internet Explorer 5.0 and later Windows
Netscape 8.0 and later Windows Macintosh
Step 2: Setup JavaScript to call from ActionScript Making the call Mozilla 1.7.5 and later Windows Macintosh
Firefox 1.0 and later Windows Macintosh
<html> …
public class ExternalInterfaceExample extends Sprite Safari 1.3 and later Macintosh
... {
<script> …
function updateForm(name, city, state) private function updateHTMLForm() ƒ To check if it is available use the isAvailable() method
{ {
name.innerHtml = name; var result:Boolean = public class ExternalInterfaceExample extends Sprite
city.innerHtml = city; ExternalInterface.call( {
state.innerHtml = state; “updateForm”, “Bob”, “Las Vegas”, public function ExternalInterfaceExample()
return true; “Nevada”); {
} if (ExternalInterface.isAvailable())
</script> if (result) …
… … }
} }
</html> …

11 12
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

2
Demo ExternalInterface

ƒ Limited reentrancy (JavaScript)

JavaScript

1 2 3
> PLACE HOLDER FOR IMAGES OF DEMO <

1 2
Fails

ActionScript

13 14
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

ExternalInterface ExternalInterface

ƒ Limited reentrancy (ActionScript) ƒ Exceptions aren’t marshaled


ActionScript Exception
JavaScript

child.graphics.beginFill(color);
if (dia == 0)
throw new ArgumentError(“Invalid diameter specified.”);
Fails child.graphics.drawCircle(dia, dia, dia);
1 2 …

1 2 3
JavaScript Exception

function updateForm(name, city, state)


{
if (name == “”)
ActionScript throw new Error(“Invalid name specified.”);
name.innerHtml = name;

}

15 16
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

FlexAjaxBridge (“the easy way”) Demo

ƒ The FlexAjaxBridge provides a thin layer of JavaScript and ActionScript that


make Flex components and Flash primitives available to JavaScript using
ExternalInterface.
Step 1: Setup Flex application
<mx:Application …>
<mx:LineChart id=“theChart” > > PLACE HOLDER FOR IMAGES OF DEMO <
<FABridge>
</mx:Application>

Step 2: Use the application within HTML


<html>

<script>
function displayDataInChart()
{
var chart = FABridge.flash.root().theChart();
chart.setDataProvider([x:10, y:20, z:50]);
}
</script>
</html>

17 18
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

3
FlexAjaxBridge (“the easy way”) Data Integration

ƒ Exceptions aren’t marshaled. ƒ Break the limitations of XML/HTTP


ƒ Reentrancy is limited ƒ Get direct access to ColdFusion, .NET, JRun, J2EE, SOAP Services, and Sockets
(XML/Binary)
ƒ Memory management is more complex.
ƒ Object references marshaled across the bridge in either direction are held
ƒ To release references call releaseASObjects()

Server
Client
Browser Server

JavaScript ASP
SOAP
<html>
… CFM
AMF/RTMP
<script> JSP
FlashPlayer (swf) XML Sockets

FABridge.flash.releaseASObjects(); Binary Sockets PHP

</script> J2EE, .NET

MySQL/MSSQL
</html>

19 20
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

Flex Data Management Services (FDMS) Ajax Client for Flex Data Services (ACFDS)

ƒ Ajax Bridge for Data Services provides access from JavaScript for messaging
Rich Client and data management services.
Applications
Domain
Model/Persistent Step 1: Load FDMSBridge into HTML page
Store
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
Flex Data Management
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
Services
<title>Products</title>
<link href="css/accordion.css" rel="stylesheet" type="text/css" />
<link href="../css/screen.css" rel="stylesheet" type="text/css" />
ƒ FDMS provides rich client applications with rich data functionality. <script type="text/javascript" src="include/FABridge.js“ />
<script type="text/javascript" src="include/FDMSLib.js“ />
ƒ Built on a robust messaging architecture
</head>
ƒ Automates data synchronization between client and server <body>
ƒ Real-time data push <script>
ƒ Occasionally connected clients FDMSLibrary.load("/ajax/Products/FDMSBridge.swf", fdmsLibraryReady);
ƒ Conflict detection, notification, and resolution
</script>

ƒ Clustering for fail over and load balancing
ƒ Publish/Subscribe messaging

21 22
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

Ajax Client for Flex Data Services (ACFDS) Ajax Client for Flex Data Services (ACFDS)

ƒ Use the data services API similarly to how it is in Flex. ƒ Remote requests are asynchronous
Step 2: Request data from the remote destination Step 3: Update HTML elements with retrieved data

<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">


… …
</body> </body>
<script language="javascript"> <script language="javascript">
var productService; function productsResult(event)
var products; {
// Once the bridge indicates that it is ready we can proceed to load the data. var htmlText = "<table id='productTable'>";
function fdmsLibraryReady() var product;
{ for (var i=0; i<products.length(); i++)
productService = new DataService("Products"); {
productService.addEventListener(DataService.RESULT, productsResult); product = products.getItemAt(i);
productService.addEventListener(DataService.FAULT, productFault); htmlText += "<tr><td>"+ product.getName() +
products = new ArrayCollection(); "</td><td>"+ product.category + "</td></tr>";
productService.fill(products); }
} htmlText += "</table>";
document.all["products"].innerHTML = htmlText;
}

23 24
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

4
Demo Ajax Client for Flex Data Services (ACFDS)

ƒ Integration with Spry


Step 1: Load FDMSLibrary into Spry HTML page and use the FDMSDataSet

<html xmlns="http://www.w3.org/1999/xhtml">

</body>
<html xmlns="http://www.w3.org/1999/xhtml">
> PLACE HOLDER FOR IMAGES OF DEMO < <head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>2006 MAX - AJAX Songs Demo</title>
<link href="SongsDemo.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="includes/spry.js"></script>
<script type="text/javascript">
var dsTopSongs = new FDMSDataSet(“Songs");
dsTopSongs.setColumnType("rank", "number");
</script>
</head>

<body>

</body>
</html>

25 26
2006 Adobe Systems Incorporated. All Rights Reserved. 2006 Adobe Systems Incorporated. All Rights Reserved.

27
2006 Adobe Systems Incorporated. All Rights Reserved.

Vous aimerez peut-être aussi