Published on

A helpful to rest with flex and some optimization

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Flex with Rest and Optimazation MyLabs Flex with Rest and Optimazation
  2. 2. Ways between AJAX and Flex using the flashVars properties, querying string parameters, using the navigateToURL() method using the ExternalInterface class
  3. 3. Using FlashVar Property <ul><li>You pass request data to Flex applications by defining the flashVars properties in the wrapper </li></ul><ul><li>The flashVars properties and URL fragments are read in the Flex application at run time. As a result, changing flashVars properties or URL fragments does not require you to recompile the Flex application. </li></ul>
  4. 4. Using FlashVar Property <html> <head> <title>/flex2/code/wrapper/SimplestFlashVarTestWrapper.html</title> <style> body { margin: 0px; overflow:hidden } </style> </head> <body scroll='no'> <table width='100%' height='100%' cellspacing='0' cellpadding='0'><tr><td valign='top'> <h1>Simplest FlashVarTest Wrapper</h1> <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab' height='100%' width='100%'> <param name='src' value='FlashVarTest.swf'/> <param name='flashVars' value='firstName=Nick&lastName=Danger'/> <embed name='mySwf' src='FlashVarTest.swf' pluginspage='http://www.adobe.com/go/getflashplayer' height='100%' width='100%' flashVars='firstName=Nick&lastName=Danger'/> </object> </td></tr></table> </body> </html>
  5. 5. Using URL fragments <ul><li>Append value to the SWF file’s URL in the HTML wrapper that embeds the Flex application. </li></ul><ul><li>http://www.domain.com/myApp.swf firstName=Marco&lastname=Casario </li></ul><ul><li>In your Flex app to access the values you use the Application.application.parameters object: </li></ul>
  6. 6. Using URL fragments <mx:Script> <![CDATA[ import mx.managers.BrowserManager; import mx.managers.IBrowserManager; import mx.utils.URLUtil; private var bm:IBrowserManager; [Bindable] private var fName:String; [Bindable] private var lName:String; private function init(e:Event):void { bm = BrowserManager.getInstance(); bm.init(&quot;&quot;, &quot;Welcome!&quot;); /* The following code will parse a URL that passes firstName and lastName as query string parameters after the &quot;#&quot; sign; for example: http://www.mydomain.com/MyApp.html#firstName=Nick&lastName=Danger */ var o:Object = URLUtil.stringToObject(bm.fragment, &quot;&&quot;); fName = o.firstName; lName = o.lastName; } ]]> </mx:Script>
  7. 7. Using the navigateToURL () method <ul><li>The navigateToURL () method loads a document from aspecific URL into a window or passes variables to another application at a defined URL </li></ul><?xml version=&quot;1.0&quot;?> <mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;> <mx:Script><![CDATA[ import flash.net.*; public function executeSearch(event:MouseEvent):void { var u:URLRequest = new URLRequest(&quot;http://www.google.com/search?hl=en&q=&quot; + ta1.text); navigateToURL(u,&quot;_blank&quot;); } ]]></mx:Script> <mx:TextArea id=&quot;ta1&quot;/> <mx:Button label=&quot;Search&quot; click=&quot;executeSearch(event)&quot;/> </mx:Application>
  8. 8. Using the navigateToURL() method <ul><li>You can use the navigateToURL() method to invoke JavaScript functions: </li></ul>/ /in Flex var u:URLRequest = new URLRequest(&quot; javascript:catchClick ('&quot;+ eName + &quot;','&quot; + eType +&quot;')&quot;); navigateToURL(u,&quot;_self&quot;); //On the HTML page <SCRIPT LANGUAGE=&quot;JavaScript&quot;> function catchClick(name, type) { alert(name + &quot; triggered the &quot; + type + &quot; event.&quot;); } </SCRIPT>
  9. 9. Using the ExternalInterface API <ul><li>The ExternalInterface is part of the flash.external package </li></ul><ul><li>This class allows developers to pass data from the Flex application to the enclosing HTML page, process it, and then pass it back to the Flex application. </li></ul><ul><li>Moreover you can call any JavaScript method on the wrapper, pass parameters, and get a return value. </li></ul><ul><li>The ExternalInterface class is the External API, an application programming interface that enables straightforward communication between ActionScript and the Flash Player container; for example, an HTML page with JavaScript, or a desktop application with Flash Player embedded. Use of ExternalInterface is recommended for all JavaScript-ActionScript communication. </li></ul>
  10. 10. Using the ExternalInterface API Access javascript from flex <![CDATA[ import mx.controls.Alert; private function init():void { var userAgent:String = ExternalInterface.call(&quot;navigator.userAgent.toString&quot;); Alert.show(userAgent, &quot;navigator.userAgent:&quot;); } ]]> </mx:Script> <mx:ApplicationControlBar dock=&quot;true&quot;> <mx:Button label=&quot;Display user agent&quot; click=&quot;init();&quot; /> </mx:ApplicationControlBar>
  11. 11. Using the ExternalInterface API Access flex from javascript public function myFunc(s:String):void { // whatever you want} public function initApp():void { ExternalInterface.addCallback(&quot;myFlexFunction&quot;,myFunc); } <SCRIPT LANGUAGE=&quot;JavaScript&quot;> function callApp() { window.document.title = document.getElementById(&quot;newTitle&quot;).value; mySwf.myFlexFunction(window.document.title); } </SCRIPT>
  12. 12. ExternalInterface API security <ul><li>Restrictions: </li></ul><ul><li>By default, scripts on the HTML page can communicate only with ActionScript in a Flex application if the page and the application are in the same domain. </li></ul><ul><li>Using the allowScriptAccess parameter (an HTML parameter) you can determine whether your Flex application can call JavaScript in the HTML page, and it applies to all functions on the page. </li></ul><object id=‘myName' classid=' clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab' allowScriptAccess='always' height='100%' width='100%'>
  13. 13. The Flex AJAX Bridge <ul><li>The Flex Ajax Bridge (FABridge) is a small, unobtrusive code library that you can insert into a Flex widget, or an empty SWF file to expose it to scripting in the web browser. It’s based on the ExternalInterface API. </li></ul><ul><li>It will save you a lot of time. </li></ul><ul><li>Using the library anything you can do with ActionScript, you can do with JavaScript. </li></ul>
  14. 14. When to use the Flex AJAX Bridge <ul><li>The FABridge library is useful in the following situations: </li></ul><ul><li>You have only one or two people on your team who know Flex. The FABridge library lets everyone on your team use the work produced by one or two Flex specialists. </li></ul><ul><li>You are building an integrated rich Internet application (RIA) with Flex and Ajax portions.Although you could build the integration yourself using ExternalInterface, you might find it faster to start with the FABridge. </li></ul>
  15. 15. Optimization <ul><li>Framework Optimization </li></ul><ul><li>Code Optimization </li></ul>
  16. 16. Framework Optimization <ul><li>Runtime Shared Libraries (RSLs) </li></ul><ul><li>Flex 1.5 introduced the concept of Runtime Shared Libraries (RSLs). With RSLs, you can reduce the size of your application’s SWF file by externalizing shared assets into standalone files that can be separately downloaded and cached on the client. These shared assets are loaded by any number of applications at runtime, but only need to be transferred to the client once. </li></ul><ul><li>If you have multiple applications but those applications share a core set of graphic files, components, and other assets, your users only have to download those assets once in an RSL. You can reduce the resulting file size for your main application dramatically. If a file in one of the RSLs changes, Flex recompiles and resends that RSL to the client. Flex does not recompile the application and other unchanged RSLs. </li></ul>
  17. 17. <ul><li>Code Optimization </li></ul>Object creation Measurement/Layout Rendering
  18. 18. Optimizing Actionscript: Object Creation <ul><li>Solution #1: Deferred Creation </li></ul><ul><li>Delay object creation until the object becomes visible </li></ul><ul><li>Is baked into Accordion, TabNavigator, and ViewStack </li></ul><ul><li>Can be added to custom containers, but subclassing ViewStack is easier </li></ul>
  19. 19. Optimizing Actionscript: Object Creation Solution #2: Ordered Creation During startup, stagger creation of objects <mx:Application> <mx:Panel width=&quot;250&quot; height=&quot;100&quot; creationPolicy=“queued” /> <mx:Label text=&quot;One&quot; /> </mx:Panel> <mx:Panel width=&quot;250&quot; height=&quot;100&quot; creationPolicy=“queued” /> <mx:Label text=&quot;Two&quot; /> </mx:Panel> </mx:Application>
  20. 20. Measurement/Layout: Definition The process of assigning a position and size to every component <mx:Application> <mx:HBox> <mx:Button label=“1”/> <mx:Button label=“2”/> </mx:HBox> <mx:TextArea width=“100%” height=“100%” text=“Text”/> </mx:Application>
  21. 21. Solution #1: Reduce Container Nesting <ul><li>Try to use HBox and VBox instead of Grid </li></ul><ul><li>Avoid nesting a VBox inside a Panel or Application </li></ul><ul><li>The root of an MXML component doesn’t need to be a Container </li></ul><ul><li>Use Canvas with constraints </li></ul><ul><li>Warning sign: a Container with a single child </li></ul>
  22. 22. Useful Links <ul><li>http://livedocs.adobe.com/flex/3/ </li></ul>