HTML與AIR之間的溝通

首先要熟記這張圖:

還有重要的官方注腳:
The JavaScript environment has its own Document and Window objects. JavaScript code can interact with the AIR run-time environment through the runtime, nativeWindow, and htmlLoader properties. ActionScript code can interact with the JavaScript environment through the window property of an HTMLLoader object, which is a reference to the JavaScript Window object. In addition, both ActionScript and JavaScript objects can listen for events disptached by both AIR and JavaScript objects.

例子: (在JS中讓AIR的native window發出事件)

JavaScript:
  1. //似乎不能用自定事件, 可惜可惜,
  2. //不然就可以直接利用事件處理把資料傳遞到AIR runtime了
  3. var e = new air.Event("foobar");
  4. window.nativeWindow.dispatchEvent( e );

例子2: ( 在AIR中監聽 Javascript DOM 事件)

Actionscript:
  1. //只能監聽DOM預設事件//
  2. HTML#htmlLoader.window.document.addEventListener("click" , onClick );

例子3: ( sandbox bridge , 取得不同sandbox的資料)

JavaScript:
  1. //在child html定義bridge
  2. var interface = {};
  3. interface.foobar = "blahblah....";
  4. window.childSandboxBridge = interface;

JavaScript:
  1. //parent html裡頭, 取得定義好的bridge
  2. var childInterface = {};
  3. childInterface = document.getElementById("myDIV").contentWindow.childSandboxBridge;
  4. air.trace( childInterface.foobar )
  5. //as console output: blahblah...//

Tags: ,

Leave a Reply