Posts Tagged ‘javascript’

AIR 跟 JS…

Sunday, July 26th, 2009

當兩者糾纏在一起的時候,如果由AIR呼叫Javascript 會掛掉的話,可先試著用setTimeout,通常都可以解決。因為HTML DOM需要時間初始化,Javascript 才能被呼叫。

像是 setTimeout( domWindow.myJsFunction , 1000 );

Drag and Drop on AIR.HTML Using NativeDragManager

Wednesday, March 4th, 2009

DOM object in HTML ( HTML.domWindow ) stop the Drag events propagating so that dragging over HTML ui will not succeed, which means you cannot see an accept-drop-icon near mouse cursor. To solve this, you have to register a handler to prevent DOM doing something behind but accept drop action, as following sample code:

myHtml.domWindow.document.addEventListener( "dragover:", onDomDragOver );
function onDomDragOver( e:* ):void
{
e.preventDefault();
NativeDragManager.acceptDragDrop( this );
}

試圖用NativeDragManager拖拉外部物件到Air時,如果遇到HTML元件,會出現無法acceptDragDrop的情況,此時,必須針對HTML#htmlLoader底下的DOM 物件監聽 dragover/dragenter 等javascript事件,阻止DOM自己的事件處理,並在此時,命令HTML接受dragDrop,請見上方原始碼。

PS: Javascript drag event types:
dragover
dragenter
drop

ex: getElementById( “myBox” ).addEventListener( “dragover” , onOverBox );

HTML與AIR之間的溝通

Wednesday, April 23rd, 2008

首先要熟記這張圖:

還有重要的官方注腳:
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發出事件)
[js]
//似乎不能用自定事件, 可惜可惜,
//不然就可以直接利用事件處理把資料傳遞到AIR runtime了
var e = new air.Event(”foobar”);
window.nativeWindow.dispatchEvent( e );
[/js]

例子2: ( 在AIR中監聽 Javascript DOM 事件)
[as]
//只能監聽DOM預設事件//
HTML#htmlLoader.window.document.addEventListener(”click” , onClick );
[/as]

例子3: ( sandbox bridge , 取得不同sandbox的資料)
[js]
//在child html定義bridge
var interface = {};
interface.foobar = “blahblah….”;
window.childSandboxBridge = interface;
[/js]

[js]
//parent html裡頭, 取得定義好的bridge
var childInterface = {};
childInterface = document.getElementById(”myDIV”).contentWindow.childSandboxBridge;
air.trace( childInterface.foobar )
//as console output: blahblah…//
[/js]

速記: Javascript@AIR

Wednesday, April 23rd, 2008

安全性解釋文件
AIR 是個跨越各種環境的應用程式,看起來很簡單,要處理的事情多很多,目前第一個遇到的問題: sandbox
sandbox 像是程式碼的“活動空間“,基本上處於不同活動空間的程式碼理當無法互相溝通,但是,必要的時候,要想辦法讓他們可以交流, ex:
sandboxBridge
sandboxRoot, documentRoot…

有關在AIR使用Javascript有一個先天上的限制,
官方手冊提到:

Loading script files from outside the application sandbox is not permitted. No security error is generated. All script files that run in the application sandbox must be installed in the application directory. To use external scripts in a page, you must map the page to a different sandbox.

想利用js作為mash-up的根基,需要許多sandbox溝通和對照的程式碼。