MXML からスクリプトを分離する方法まとめ(4種類)
ここ数日 Flex をいじっていて、 MXML からスクリプトを分離する方法を調べてたらいろいろ出てきたのでまとめ*1。使用環境は FlashDevelop 4.0.1 RTM + Flex SDK 4.6.0 + Windows XP Pro 32bit 。
以下で紹介するのは以下の4種類。結論を先に書くと mx.core.IMXMLObject インターフェースを実装する方法がおすすめ。
<fx:Script>
の source 属性を使う or include する方法mx.core.IMXMLObject
インターフェース実装を使う方法mx.core.FlexGlobals.topLevelApplication
を参照する方法mx.core.Application
の継承クラスを使う方法
分離する前の状態
まず <fx:Script><![CDATA[ ... ]]></fx:Script>
内に処理を書いている状態を想定。ここからロジックを分離していく。
プロパティをビューにバインドしてコントロールにイベントをアタッチできれば大概のことはできるだろう、ということで処理的なものはバインドとイベントアタッチのみ。
HelloWorld/ ├── bin/ ├── lib/ └── src/ └── HelloWorld/ └── Main.mxml
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="onCreationComplete()"> <fx:Script> <![CDATA[ [Bindable] private var statusMsg:String = "initialized."; private function onCreationComplete():void { statusMsg = "Creation complete."; } private function onButtonClick():void { statusMsg = "Clicked!"; } ]]> </fx:Script> <s:HGroup> <s:Label text="Status: {statusMsg}"/> <s:Button label="Click me" click="onButtonClick()"/> </s:HGroup> </s:Application>
*1:こういうのは Flash Builder 使うとバシバシ自動生成してくれるのかもしれないけど使ったことないのでわかりません><
CentOS 5.7 + Passenger
CentOS 5.7 に Passenger を入れて Rack アプリを動かしたメモ。
開発元が用意するリポジトリから mod_passenger と rubygem-passenger を入れる。
参考:Phusion Passenger native packages for RedHat/Fedora/CentOS – Phusion Corporate Blog
# rpm -Uvh http://passenger.stealthymonkeys.com/rhel/5/passenger-release.noarch.rpm # yum install mod_passenger rubygem-passenger # rpm -qa \*passenger mod_passenger-3.0.11-9.el5.centos rubygem-passenger-3.0.11-9.el5.centos
Apache モジュールをインストール。
# passenger-install-apache2-module
手元の環境ではここで curl-devel と ruby-devel が足りないよと言われたので、手動で入れてやりなおし。
# yum install curl-devel.x86_64 ruby-devel.x86_64 # passenger-install-apache2-module
途中で表示されたバーチャルホストの設定例をメモ( /etc/httpd/conf.d/passenger.conf 内でも確認できる)。
<VirtualHost *:80> ServerName www.yourhost.com DocumentRoot /somewhere/public # <-- be sure to point to 'public'! <Directory /somewhere/public> AllowOverride all # <-- relax Apache security settings Options -MultiViews # <-- MultiViews must be turned off </Directory> </VirtualHost>
どうやら以下のようなディレクトリ構成にすると勝手に動いてくれるらしい。
参考:PassengerでRackアプリを動かす - As Sloth As Possible
/path/to/example.jp/rackhello/ +- hello.rb +- config.ru +- public/ <- ここを DocumentRoot にする。空でもOK。
vhost を設定する前に Hello World を用意。
$ mkdir -pv /path/to/example.jp/rackhello/public $ vi /path/to/example.jp/rackhello/hello.rb
require 'rubygems' require 'rack' class HelloApp def call(env) [200, {"Content-Type" => "text/plain"}, ["Hello, Rack"]] end end
$ vi /path/to/example.jp/rackhello/config.ru
require './hello.rb' run HelloApp.new
で Apache を設定。
$ su - # vi /etc/httpd/conf.d/vhost.jp.example.conf
<VirtualHost *:80> ServerName example.jp DocumentRoot /path/to/example.jp/rackhello/public ErrorLog logs/example.jp-error_log CustomLog logs/example.jp-access_log common ServerAdmin webmaster@example.jp </VirtualHost>
# /etc/init.d/httpd configtest Syntax OK # /etc/init.d/httpd reload Reloading httpd: [ OK ]
楽だー。