2010-02-22 55 views
0

我在前端應用程序引擎中使用了python和webapp框架,在後端使用了flex 4。 我想傳遞一個字符串形式後端前端,所以我寫入以下代碼在main.py:如何將數據從Google App Engine(Python)傳遞到Flex 4應用程序

class MainPage(webapp.RequestHandler): 

def get(self): 

    userVO = "test" 

    template_values = { 
    'url': self.request.uri, 
    'userVO': userVO, 

    } 
    self.response.out.write(template.render("templates/index.html", template_values)) 

而在柔性4,I有以下代碼:

var user:String = FlexGlobals.topLevelApplication.parameters['userVO']; 

但是,我收到空值。

請指教如何改正它。謝謝。

編輯:2月25日

感謝誰回答我的問題的人。對於我的問題,我試圖弄清楚當應用程序渲染包含swf文件的html文件時,python應用程序引擎如何將數據傳遞給flex應用程序。也許,我可以在main.py,swfobject.js或index.html中設置一些內容來完成我的任務。

我知道如何使用Pyamf作爲服務Flex應用程序的網關,我正在考慮如何讓應用程序變得更簡單。

謝謝。

編輯:2月28日

羅伯特中的index.html是由Flash Builder 4中創建的標準文件希望你能給我一些提示如何修改它。以下是該文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!-- saved from url=(0014)about:internet --> 

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
<!-- 
Smart developers always View Source. 

This application was built using Adobe Flex, an open source framework 
for building rich Internet applications that get delivered via the 
Flash Player or to desktops via Adobe AIR. 

Learn more about Flex at http://flex.org 
// --> 
    <head> 
    <title></title>   
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 

HTML,身體{高度:100%; } body {margin:0;填充:0;溢出:隱藏;文本對齊:中心; }
#flashContent {display:none; }

<script type="text/javascript" src="/js/swfobject.js"></script> 
    <script type="text/javascript"> 
     <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
     var swfVersionStr = "10.0.0"; 
     <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. --> 
     var xiSwfUrlStr = "/swfs/playerProductInstall.swf"; 
     var flashvars = {}; 
     var params = {}; 
     params.quality = "high"; 
     params.bgcolor = "#ffffff"; 
     params.allowscriptaccess = "sameDomain"; 
     var attributes = {}; 
     attributes.id = "index"; 
     attributes.name = "index"; 
     attributes.align = "middle"; 
     swfobject.embedSWF(
      "/swfs/index.swf", "flashContent", 
      "100%", "100%", 
      swfVersionStr, xiSwfUrlStr, 
      flashvars, params, attributes); 

swfobject.createCSS( 「#flashContent」, 「顯示:塊;文本對齊:左;」);

要查看此頁面確保Adobe Flash Player版本 10.0.0或更高安裝。

Get Adobe Flash Player

<noscript> 
     <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="index"> 
      <param name="movie" value="index.swf" /> 
      <param name="quality" value="high" /> 
      <param name="bgcolor" value="#ffffff" /> 
      <param name="allowScriptAccess" value="sameDomain" /> 
      <!--[if !IE]> 
      <object type="application/x-shockwave-flash" data="index.swf" width="100%" height="100%"> 
       <param name="quality" value="high" /> 
       <param name="bgcolor" value="#ffffff" /> 
       <param name="allowScriptAccess" value="sameDomain" /> 
      <![endif]--> 
      <!--[if gte IE 6]> 
      <p> 
       Either scripts and active content are not permitted to run or Adobe Flash Player version 
       10.0.0 or greater is not installed. 
      </p> 
      <![endif]--> 
       <a href="http://www.adobe.com/go/getflashplayer"> 
        <img src="https://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" /> 
       </a> 
      <!--[if !IE]> 
      </object> 
      <![endif]--> 
     </object> 
</noscript> 

謝謝,羅伯特。

編輯:3月7日。

羅伯特,

參考http://help.adobe.com/en_US/Flex/4.0/html/WS2db454920e96a9e51e63e3d11c0bf626ae-7feb.html

之前,我在這裏發表的問題,我嘗試下面的代碼:

在index.html的,

<% 
    String user = (String) request.getParameter("userVO"); 
%> 

,也

flashvars.userVO = "<%= user %>"; 

結果,我得到:

<用戶

你知道爲什麼我不能得到正確的數據。謝謝。

回答

1

根據您對James Ward的回覆的評論,我想知道您是否可以通過FlashVars參數元素完成您的需求?

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html

你可能只需要調整的index.html模板打造的Flash變數param元素。

編輯:3月6日

你可以嘗試尋找: http://polygeek.com/801_flex_reading-flashvars-in-flex

你需要確保你等到應用程序已經加載訪問Flash變量。

編輯:3月7日

正確的,在這些實施例他們硬編碼的值。您需要編輯模板,以便將值設置爲模板參數的值。

所以,index.html中: flashvars.userVO = "{{ userVO }}"

+0

感謝Robert,我知道我可以調整Flashvars來完成任務,並且我已經嘗試了很多次。但是,它不成功。對於livedocs,Flex 3和4,我已經讀過好幾次了。 – michael 2010-02-27 14:25:07

+0

你可以發佈你的index.html模板的內容嗎? – 2010-02-27 16:22:30

+0

羅伯特,我在問題中發佈文件。 – michael 2010-02-28 16:13:15

2

從Flex到GAE的最佳交談方式是使用AMF。這裏是如何:

的app.yaml

application: flexandthecloud 
version: 3 
runtime: python 
api_version: 1 

handlers: 
- url: /services/.* 
    script: main.py 

main.py

#!/usr/bin/env python 
import wsgiref.handlers 

from pyamf.remoting.gateway.wsgi import WSGIGateway 

def sayHello(name): 
    return "howdy " + name 

services = { 
    'services.sayHello': sayHello 
} 

def main(): 
    application = WSGIGateway(services) 
    wsgiref.handlers.CGIHandler().run(application) 

if __name__ == '__main__': 
    main() 

Flex 3的代碼(可以很容易地修改針對Flex 4):

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 

    <mx:RemoteObject id="ro" destination="services" endpoint="http://flexandthecloud.appspot.com/services/"> 
     <mx:result> 
      l.text = event.result as String; 
     </mx:result> 
    </mx:RemoteObject> 

    <mx:TextInput id="ti"/> 
    <mx:Label id="l"/> 
    <mx:Button label="say hello" click="ro.sayHello(ti.text)"/> 

</mx:Application> 
+0

感謝詹姆斯。其實,我正在使用pyamf作爲網關。我試圖將一些數據傳遞給flex應用程序,當html改變時,我不需要調用任何服務,我可以獲取一些數據。 – michael 2010-02-22 15:54:03

+0

我只會在應用程序上使用applicationComplete事件來調用服務並加載數據。 – 2010-02-23 14:09:02

+0

是的,你可以,但你需要再次連接應用引擎。這會增加開銷。 – michael 2010-02-23 14:28:41

1

是什麼在你的index.html? 可以傳遞到index.html中的數值,設置像一個JavaScript函數:

function passvalue { 
    PassParameter("userVO", "{{userVO}}"); 
} 

然後設置Flex中的函數:

public function gethtmlparam(name:String, val:String):void { 
      switch (name) { 
       case "userVO": userVO= val; break;} 
} 

和回撥這兩個功能:

ExternalInterface.addCallback("PassParameter", gethtmlparam); 

希望它能提供幫助。

+0

mal39,謝謝你的回覆。這是一個很好的嘗試。不過,我正在測試python應用服務器如何通過swfobject將數據傳遞給flex應用程序。也許,我的問題還不夠清楚。 對於index.html,它是由Flash Builder自動生成的標準文件。無論如何,謝謝你的建議。 – michael 2010-02-25 07:22:15

相關問題