2008. 8. 20. 11:31
[Flex3] ActionScript 와 MXML 을 분리하는 기술 / Techniques for separating ActionScript from MXML
2008. 8. 20. 11:31 in Adobe/Flex

Reference : http://livedocs.adobe.com/flex/3/html/usingas_6.html#216704
하나의 함수를 호출하는 아래의 어플리케이션으로 MXML 에서 ActionScript 를 분리하는 몇 가지 방법에 대해 보여줄 것이다.
Temperature 어플리케이션은 하나의 인풋 필드에서 인풋을 받고 인풋을 Farenheit에서 Celsius 로 변환하는 함수를 사용한다. 그러면 Label 컨트롤에 결과로 나온 온도를 보여준다.
아래의 이미지는 Temperature 어플리케이션을 보여준다:
One MXML document (event handling logic in event attribute)
아래의 코드는 MXML 태그의tag's click 이벤트 내부의 ActionScript 이벤트 처리 로직을 보여준다:
<?xml version="1.0"?>
<!-- usingas/ASOneFile.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert"
click="celsius.text=String(Math.round((Number(fahrenheit.text)-32)/1.8 * 10)/10);"/>
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
이 코드를 실행한 파일은 아래와 같다:
<!-- usingas/ASOneFile.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert"
click="celsius.text=String(Math.round((Number(fahrenheit.text)-32)/1.8 * 10)/10);"/>
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
이 코드를 실행한 파일은 아래와 같다:
One MXML document (event handling logic in <mx:Script> block)
이 예에서 함수 로직은 MXML 문서의 <mx:Script> 블럭 내에 위치하고 MXML 태그의 click 이벤트에서 호출된다:
<?xml version="1.0"?>
<!-- usingas/ASScriptBlock.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
public function calculate():void {
var n:Number = Number(fahrenheit.text);
var t:Number = Math.round((n-32)/1.8*10)/10;
celsius.text=String(t);
}
]]></mx:Script>
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert" click="calculate();" />
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
<!-- usingas/ASScriptBlock.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
public function calculate():void {
var n:Number = Number(fahrenheit.text);
var t:Number = Math.round((n-32)/1.8*10)/10;
celsius.text=String(t);
}
]]></mx:Script>
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert" click="calculate();" />
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
One MXML document and one ActionScript file (event handling logic in separate script file)
여기서 함수 호출은 MXML 이벤트
<?xml version="1.0"?>
<!-- usingas/ASSourceFile.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- 함수가 있는 ActionScript 파일 명시. -->
<mx:Script source="includes/Sample3Script.as"/>
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert" click="celsius.text=calculate(fahrenheit.text);"/>
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
코드를 실행한 결과는 아래와 같다:
<!-- usingas/ASSourceFile.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<!-- 함수가 있는 ActionScript 파일 명시. -->
<mx:Script source="includes/Sample3Script.as"/>
<mx:Panel title="My Application" paddingTop="10" paddingBottom="10"
paddingLeft="10" paddingRight="10">
<mx:HBox>
<mx:Label text="Temperature in Fahrenheit:"/>
<mx:TextInput id="fahrenheit" width="120"/>
<mx:Button label="Convert" click="celsius.text=calculate(fahrenheit.text);"/>
<mx:Label text="Temperature in Celsius:"/>
<mx:Label id="celsius" width="120" fontSize="24"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
코드를 실행한 결과는 아래와 같다:
Sample3Script.as ActionScript 파일에 아래의 코드가 있다:
// usingas/includes/Sample3Script.as
public function calculate(s:String):String {
var n:Number = Number(s);
var t:Number = Math.round((n-32)/1.8*10)/10;
return String(t);
}
public function calculate(s:String):String {
var n:Number = Number(s);
var t:Number = Math.round((n-32)/1.8*10)/10;
return String(t);
}
'Adobe > Flex' 카테고리의 다른 글
[Flex3] 어플리케이션 개발 / Developing applications (0) | 2008.08.27 |
---|---|
[Flex3] MXML 에 대하여 / About MXML (0) | 2008.08.26 |
[Flex3] Tile 레이아웃 컨테이너 / Tile layout container (0) | 2008.08.21 |
[Flex3] Panel 레이아웃 컨테이너 / Panel layout container (0) | 2008.08.21 |
[Flex3] ActionScript 컴포넌트 생성 / Creating ActionScript components (0) | 2008.08.20 |
[Flex3] Comparing, including, and importing ActionScrip code (0) | 2008.08.19 |
[Flex3] Flex 컴포넌트 / Working with Flex Components (0) | 2008.08.17 |
[Flex3] Flex 어플리케이션에서 ActionScript 사용 / Using ActionScript in Flex applications (0) | 2008.08.16 |
[Flex3] ActionScript 사용 / Using ActionScript (0) | 2008.08.16 |
[Flex3] Form, FormHeading, FormItem 레이아웃 컨테이너 / Form, FormHeading, and FormItem layout containers (0) | 2008.08.13 |