Reputation: 505
I notice that bindable public vars are not available between views, what is the best practice for seeing a central set of variables across an entire application.
Regards and thanks in advance for the help. Craig
Upvotes: 0
Views: 116
Reputation: 96
ran into this question accidentally and maybe i have an answer. Even if the question itself is a few months old.
I get around your problem by using a model. Using the MVC pattern you can have a "repository" of variables always available to your application no matter where. After you master, or feel comfortable, with the pattern, you can be flexible on how many models you have, according to your project specifications. Forgive the simplicity, because you can, and should, plan your structure well to accomodate for your project.
As a simple example:
. Lets assume you have 2 views: "v1" and "v2"
. We create a singleton model - lets call it "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
. If you want to use functions or variables from this model in any of your views:
import modelo.myNinjaModel;
[Bindable]
private var smodelo:AModel=AModel.instance;
. if you want to access your variable, you do this:
trace(smodelo.myNameAnywhere); // this will output lawrence waterhouse
Sorry if i mistyped something, i am doing this by heart.
Hope this helped in any way, although i assume you must have more than solved your problem. :-)
You can read a bit about MVC here (theory):
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Upvotes: 1