2012-10-05 54 views

回答

1

偶然遇到這個問題,也許我有一個答案。即使問題本身是幾個月前。

我通過使用模型解決了您的問題。使用MVC模式,無論在哪裏,都可以爲應用程序提供始終可用的變量「存儲庫」。根據您的項目規格,在掌握或感覺舒適的模式後,您可以靈活確定您擁有的模型數量。原諒簡單性,因爲你可以並且應該很好地規劃你的結構,以適應你的項目。

作爲一個簡單的例子:

。讓我們假設您有2個視圖:「v1」和「v2」

。我們創建一個單例模型 - 可以稱之爲「myNinjaModel.as」:

package modelo 
{ 
// your imports here 


[Bindable] 
public class myNinjaModel 

{ 



private static const _instance:myNinjaModel = new myNinjaModel(SingletonLock); 


public static function get instance():myNinjaModel 
{ 
return _instance; 
} 

public function myNinjaModel(lock:Class) 
{ 
// Verify that the lock is the correct class reference. 
if (lock != SingletonLock) 
{ 

throw new Error("Invalid Singleton access. Use Model.instance."); 

} 


} 

// you can add some function here that you can access from anywhere in your application 
// but be aware of data/logic separation, etc. 


// also some nice variables here 

var myNameAnywhere:String = "lawrence waterhouse"; 




} // end class 
} // end package 


class SingletonLock 
{ 
} // end class 

。如果您想在任何視圖中使用此模型的功能或變量:

import modelo.myNinjaModel; 

[Bindable] 
private var smodelo:AModel=AModel.instance; 

。如果你想訪問你的變量,你這樣做:

trace(smodelo.myNameAnywhere); // this will output lawrence waterhouse 

對不起,如果我打錯了什麼,我是這樣做的心。

希望這有助於任何方式,但我認爲你必須有更多的解決你的問題。 :-)

你可以閱讀一些關於MVC在這裏(理論):

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

相關問題