Development Environment:
OS: Windows XP
RUBY: ruby1.9.1
Rails: rails2.3.5
IDE: rubymine2.0.1
Flash builder: Flash builder4
Background:
In ruby-Flex practice-using swfobject to load SWF in a ruby project describes how to reference SWF on the rails interface. This example describes how to pass parameters from the rails page to the flex application.
The parameter types analyzed in this example include string, array, and hash.
(Note: to parse JSON data in flex, reference the following lib to download as2corelib)
One-click revision of flex.html in the previous step
The modified code is as follows:
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" <br/> "http://www.w3.org/TR/html4/loose.dtd"> <br/> <HTML> <br/> <pead> <br/> <title> flex on Rails </title> <br/> <MCE: script Type = "text/JavaScript" src = "/javascripts/swfobject. JS "mce_src =" javascripts/swfobject. JS "> </MCE: SCRIPT> <br/> <MCE: Script Type =" text/JavaScript "> <! -- <Br/> for Version Detection, set to Min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> <br/> var swfversionstr = "10.0.0"; <br/> <! -- To use express install, set to playerproductinstall.swf, otherwise the empty string. --> <br/> var xiswfurlstr = "/SWF/playerproductinstall.swf"; <br/> var flashvars ={}; <br/> flashvars. name = "Crystal"; <br/> flashvars. message = "hello"; <br/> flashvars. bookinventory = ["apple", "Pear", "grape"]; <br/> flashvars. hash = "[{'title': 'History ', 'price': 19}]"; <br/> var Params ={}; <br/> Params. quality = "High"; <br/> Params. bgcolor = "# ffffff"; <br/> Params. allowScriptAccess = "samedomain"; <br/> Params. allowfullscreen = "true"; <br/> var attributes ={}; <br/> attributes. id = "railsproject"; <br/> attributes. name = "railsproject"; <br/> attributes. align = "Middle"; <br/> swfobject. embedswf (<br/> "/SWF/railsproject.swf", "flashcontent", <br/> "100%", "100%", <br/> swfversionstr, xiswfurlstr, <Br/> flashvars, Params, attributes); <br/> <! -- Javascript enabled so display the flashcontent DIV in case it is not replaced with a SwF object. --> <br/> swfobject. createcss ("# flashcontent", "display: block; text-align: Left;"); </P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <Div id = "flashcontent"> <br/> <p> <br/> to view this page ensure that Adobe Flash Player version <br/> 10.0.0 or greater is installed. <br/> </P> <br/> <MCE: script ty Pe = "text/JavaScript"> <! -- <Br/> var pagehost = (document. Location. Protocol = "https :")? "Https: //": "http: //"); <br/> document. write ("<a href =" http://www.adobe.com/go/getflashplayer "mce_href =" http://www.adobe.com/go/getflashplayer "> + pagehost +" alias "mce_src =" & quot; <br/> + pagehost + & quot; www.adobe.com/images/shared/download_buttons/get_flash_player.gif "alt = 'get Adobe Flash player'/> </a> "); </P> <p> // --> </MCE: SCRIPT> <br/> </div> <br/> </body> <br/> </ptml>
Ii. Modify the flex application railsproject. mxml
The modified code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <s: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" <br/> xmlns: S = "Library: // ns.adobe.com/flex/spark" <br/> xmlns: MX = "Library: // ns.adobe.com/flex/mx" minwidth = "955" minheight = "600" <br/> creationcomplete = "Init ()"> <br/> <FX: declarations> <br/> <! -- Place non-visual elements (e.g ., services, value objects) Here --> <br/> </FX: declarations> <br/> <FX: SCRIPT> <br/> <! -- [CDATA [<br/> Import COM. adobe. serialization. JSON. JSON; <br/> Import MX. controls. alert; </P> <p> [Bindable] <br/> var gdprovider: array; <br/> private function Init (): void {<br/> // pass the hash array. the received data type is string, and the transmitted data is [{'title': 'History ', 'price ': 19}], JSON. during decode processing, you must first convert ''' to '"' <br/> // Question 1: directly transmit [{" title ":" History ", "price": 19}] Then MX. core. application. application. parameters. hash can only obtain '[{' <br/> // Therefore, only ''' is passed during transmission, and R is performed on the flex end. Eplace processing <br/> // problem 2: string. replace (''', "/" ") can only Replace the first ''' with '"'. Therefore, use string. split ("'"). join ("/" ") to replace all ''' with" <br/> var STR: String = mx. core. application. application. parameters. hash. tostring (). split ("'"). join ("/" "); </P> <p> // convert the parsed data to an array for table display. <br/> GD. dataprovider = JSON. decode (STR) as array; <br/>}</P> <p> private function getstringparams (): void {<br/> // pass the string <br/> var name: String = mx. core. application. Application. parameters. name; <br/> var message: String = mx. core. application. application. parameters. message; <br/> strtext. TEXT = Name + "," + message; <br/>}< br/> private function getarrayparams (): void {<br/> // transmits an array, the received data type is string, the separator is ',', and the data is flashvars during transmission. bookinventory = ["1", "2", "3"] <br/> var array: array = (MX. core. application. application. parameters. bookinventory ). tostring (). split (','); <br/> for (var I: num BER = 0; I <array. length; I ++) <br/> arrtext. text + = array [I] + ","; <br/>}< br/>] --> <br/> </FX: SCRIPT> <br/> <s: panel X = "380" Y = "118" width = "461" Height = "303" Title = "Welcome to study flex on rails! "> <Br/> <s: label x = "10" Y = "10" text = "hash data" width = "223" Height = "16"/> <br/> <s: button x = "339" Y = "239" label = "get string Param" Click = "getstringparams ()"/> <br/> <mx: dataGrid x = "-1" Y = "30" id = "GD" width = "100%" Height = "133"/> <br/> <s: button x = "339" Y = "200" label = "Get array Param" Click = "getarrayparams ()"/> <br/> <s: textinput x = "71" Y = "200" id = "arrtext" width = "244"/> <br/> <s: label x = "3" Y = "206" text = "array data"/> <br/> <s: textinput x = "71" Y = "238" id = "strtext" width = "244"/> <br/> <s: label x = "3" Y = "244" text = "string data"/> <br/> </S: Panel> <br/> </s: application> <br/>
Demo effect: