簡體   English   中英

使用參數中的復雜對象從AS3調用JS函數

[英]JS Function Invocation from AS3 with Complex Object in Parameter

我正在構建AIR桌面應用程序。 在某一時刻,應用程序加載一個彈出窗口(基於s:Window的MXML組件),該窗口包含一個mx:HTML組件,該組件加載本地(在應用程序目錄中)html文件blank.html blank.html中的相關元素是:

<script src="jw/jwplayer.js"/> <!--JW Player's JS-based Embedder-->
...
<div id="jwtarget" /> <!-- the target that the embedder will use -->

由於我要使用的參數是在運行時確定的,因此我使用domWindow屬性調用加載播放器的方法。 這是一個有效的示例:

private function injectPlayer():void {
  var playerVars:Object = {};
  playerVars.flashplayer = "jw/player.swf";
  playerVars.file = "http://www.archive.org/download/meet_john_doe_ipod/meet_john_doe_512kb.mp4";
  playerVars.height = 360;
  playerVars.width = 640;

  try { // attempt to invoke the js function
    htmlComponent.domWindow.jwplayer("jwtarget").setup(playerVars);
  } catch(e:Error) {}

}

頁面加載完成時通過以下方式調用:

<mx:HTML id="htmlComponent" location="assets/blank.html" complete="injectPlayer()" />

一切正常。

現在到問題。 我需要能夠將一個更復雜的playerVars Object傳遞給該函數,但是我似乎語法不正確。 這是我嘗試過的最簡單的示例:

private function injectPlayer():void {
  var playerVars:Object = {};
  //playerVars.flashplayer = "jw/player.swf";
  playerVars.file = "http://www.archive.org/download/meet_john_doe_ipod/meet_john_doe_512kb.mp4";
  playerVars.height = 360;
  playerVars.width = 640;
  playerVars.modes = [{"type":"flash","src":"jw/player.swf"}];

  try { // attempt to invoke the js function
    htmlComponent.domWindow.jwplayer("jwtarget").setup(playerVars);
  } catch(e:Error) {}

}

此代碼應創建與上面的代碼完全相同的東西,但是無法執行。 我假設我需要以某種方式更改語法,以允許將Objects( modes )數組作為參數正確傳遞給js函數。

我已經嘗試了各種方法,例如將modes作為String傳遞,或首先將整個內容通過JSON.stringify() ,但無濟於事。 有人知道為參數構造復雜對象的正確方法嗎?

其他詳細信息(如果您現在尚未推斷出這些信息):Flex 4.5.1是我正在使用的SDK,包括AIR 3.0擴展(這意味着定位FP11)。

更新:

我嘗試過的另一種配置可以正常工作:

playerVars.modes = {"type":"flash", "src":"jw/player.swf"};

但是,這仍然不能解決我應該能夠在modes屬性中傳遞對象數組的問題。 但是至少這樣可以加載視頻播放器。

更多更新:

因此,我從jwplayer.js中發現了這小段代碼,我懷疑播放器加載失敗:

if (typeof parsedConfig.modes == "string") {
  _modes = _playerDefaults();
  _modes[0].src = parsedConfig.modes;
} else if (parsedConfig.modes instanceof Array) {  // I suspect this was eval'd as false
  _modes = parsedConfig.modes;
} else if (typeof parsedConfig.modes == "object" && parsedConfig.modes.type) {
  _modes = [parsedConfig.modes];
}

為了證實我的懷疑,我在blank.html中添加了以下功能:

<script type="text/javascript">
  var instanceOfArrayTest = function(arr) {
    return arr instanceof Array;
  }
</script>

在我的ActionScript代碼中嘗試了以下操作:

trace([1,2,3] is Array); // true
trace(htmlComponent.domWindow.instanceOfArrayTest([1,2,3])); // false!!!!

因此,似乎問題在於ActionScript沒有將AS3 Array對象作為JS Array對象傳遞!

嘗試這樣做:

playerVars.modes = [{type:"flash",src:"jw/player.swf"}];

ExternalInterface類的call()方法不同,當將mx:HTML作為參數傳遞給JS函數時,它們不會自動將AS3類轉換為相應的JS類。 相反, HTML控件維護一個環境,在該環境中,AS3類的本機方法和屬性得以保留,並可以直接由JS訪問。

如果JS函數需要一個JS Array對象,則必須使用JavaScript Window對象顯式創建JS Array,以訪問JS Array構造函數。 HTML控件通過其domWindow屬性提供對此的訪問。 否則,無法將AS3陣列“投射”到JS陣列。

這是一個基本示例:

var JSArray:Function = htmlComponent.domWindow.Array;
htmlComponent.domWindow.instanceOfArrayTest( JSArray(1,2,3) ); // true

對於使用JW Player的config參數的更復雜的示例:

playerVars.modes = JSArray({"type":"flash","src":"jw/player.swf"},{"type":"html5"});

這將創建一個包含兩個對象的JS數組。

有關HTML控件中JavaScript環境的更多信息,請查看Adobe的“ 使用Flex開發AIR應用程序”的 AIR部分中的JavaScript

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM