Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Why do I put VO in proxy constructor?  (Read 9531 times)
broadysmile
Newbie
*
Posts: 7


View Profile Email
« on: February 12, 2011, 09:43:39 »

As in topic, I didn't yet discover it so I'm a little afraid of doing something wrong here:

public var so:SharedObject = SharedObject.getLocal('id');      
public function SharedObjectProxy()
{
   super(NAME, so.data);
}

That's why I ask, what does PureMVC do with the VO, what can I put there and what I shouldn't?

Thanks in advance.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: February 12, 2011, 10:53:17 »

The second parameter of the Proxy class constructor is the data, type Object. It can be anything at all. It becomes the instance's data property. We usually also include a typed getter for the data which may or may not be public, depending on whether you want to directly expose the data object to the rest of the app, or only allow it to be manipulated via other public methods on the proxy.

So a SharedObjectProxy would presumably have as its data a local shared object. You don't need to create and initialize a separate property for it. And since loading it may fail, you really shouldn't set it in the constructor, but instead, wait until onRegister where you can try/catch and send the error message off in a notification if it fails. Also, since you may access multiple LSOs in a single program, you may want to make your SharedObjectProxy able to serve all of them by registering multiple instances of the SharedObjectProxy, each with a unique name.

:
// proxy name
public static const NAME:String = "SharedObjectProxy";

// note names
public static const ERROR:String = NAME+"/error";

// SharedObject names
public static const USER:String = "user";
public static const ACCT:String = "account";
public static const PREFS:String = "prefs";

public function SharedObjectProxy( soName:String )
{
   // multiple instances of this proxy may be registered
   // so the name must be unique (use the SharedObject name constants above)
   super( getProxyNameForSO( soName ) );
}

public function onRegister():void
{
   // wait till proxy is registered to try and get the SO,
   // since it could fail, you need to be able to report it.
   try {
      data = SharedObject.getLocal( soName );
   } catch ( error:Error ) {
      sendNotification( ERROR, error );
   }
}

// get the shared object name
private function get soName():String
{
    // parse it out of the proxy name.
    return ( Array( getProxyName.split( "/" ) )[1];
}

// only proxy has direct access to SharedObject
private function get so():SharedObject
{
   return data as SharedObject;
}

// save the shared object
public function saveSO():void
{
   try {
      so.flush;
   } catch ( error:Error ) {
      sendNotification( ERROR, error );
   }
}

// clear the shared object
public function clearSO():void
{
   so.clear;
   saveSO();
}

// get the User
public function get user():User
{
   return ( so.data.hasOwnProperty (USER ) )?User( so.data[USER] ):null;
}

// set the User
public function set user( u:User ):void
{
   so.data[USER] = u;
   saveSO();
}

// get the Account
public function get account():Account
{
   return ( so.data.hasOwnProperty( ACCT ) )?Account( so.data[ACCT] ):null;
}

// set the Account
public function set account( a:Account ):void
{
   so.data[ACCT] = a;
   saveSO();
}

// get the Preferences
public function get prefs():Preferences
{
   return ( so.data.hasOwnProperty( PREFS ) )?Preferences( so.data[PREFS] ):null;
}

// set the Preferences
public function set prefs( p:Preferences ):void
{
   so.data[PREFS] = p;
   saveSO();
}

public static function getProxyNameForSO( soName:String )
{
   return NAME+"/"+soName;
}

Then when you prepare your Model, you'd do something like this:

:
// Proxy for User SharedObject
var userSOProxy:SharedObjectProxy = new SharedObjectProxy(SharedObjectProxy.USER);
facade.register(userSOProxy);

// Proxy for Account SharedObject
var acctSOProxy:SharedObjectProxy = new SharedObjectProxy(SharedObjectProxy.ACCT);
facade.register(acctSOProxy);

// Proxy for Preferences SharedObject
var prefsSOProxy:SharedObjectProxy = new SharedObjectProxy(SharedObjectProxy.PREFS);
facade.register(prefsSOProxy);

And when you want to retrieve one and work with it:
:
var prefsProxyName:String = SharedObjectProxy.getProxyNameForSO(SharedObjectProxy.PREFS);
var prefsSOProxy:SharedObjectProxy = facade.retrieveProxy( prefsProxyName ) as SharedObjectProxy.
var prefs:Preferences = prefsSOProxy.prefs;
prefs.homepage = "http://about.me/trickyelf";
prefsSOProxy.saveSO();

Hope this helps,
-=Cliff>
Logged
broadysmile
Newbie
*
Posts: 7


View Profile Email
« Reply #2 on: February 12, 2011, 11:20:20 »

Thank You, Mr. Cliff, You are a pure awesomness!
Logged
Pages: [1]
Print