puremvc
|
|
« 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>
|