Skip to content

Settings.as #6

@stevenalanstark

Description

@stevenalanstark

Hello,

Settings.as seems incomplete. I had to re-write for my purposes, here is my version:



package com.xjsfl.jsfl.modules 
{
    import flash.net.SharedObject;
    import com.xjsfl.utils.debugging.Output;

    /**
     * A Model who's storage mechanism is the Shared Object
     * The allows the model to synchronously store and reteive up to 
     * 100K of data between sessions. The data cannot be moved from 
     * machine to machine
     * @author Dave Stewart
     */
    public dynamic class Settings
    {
        // ---------------------------------------------------------------------------------------------------------------------
        // { region: Variables

            // properties
                protected var _name         :String
                protected var _so           :SharedObject;


        // ---------------------------------------------------------------------------------------------------------------------
        // { region: Instantiation

            /**
             * A SettingsModel has a 1:1 relationship with a Shared object, and is used to 
             * store up to 100K of persistent data between sessions. Useful for saving small
             * amounts of data that doesn't need to be transferrable, i.e. user interface
             * settings, and so on
             * @param   A string defining the unique name of the SharedObject
             * @param   An optional 
             */
            public function Settings(name:String, data:* = null) 
            {
                // properties
                    this._name  = name;
                    this._so    = SharedObject.getLocal(name.replace(/\W/g, '_'));

                // initialize with data
                    if (data)
                    {
                        load(data);
                    }


                for (var i:String in this._so.data)
                {
                    this[i] = this._so.data[i];
                }
            }


        // ---------------------------------------------------------------------------------------------------------------------
        // { region: Public Methods

            public function load(data:*):void 
            {
                for (var i:String in data)
                {
                    this[i] = data[i];
                }
            }

            public function save():Boolean
            {
                for (var i:String in this)
                {
                    if( !( this[i] is SharedObject ) && ( i != "_name" ) )
                        _so.data[i] = this[i];
                }
                _so.flush();
                return true;
            }

            public function clear():Boolean
            {
                _so.clear();
                _so.flush();
                return true;
            }

            /**
             * Returns a hierarchical representation of the model
             * @return
             */
            public function toString():String  
            {
//              var obj:Object = { };
//              for (var i:String in this)
//              {
//                  obj[i] = this[i];
//              }
//              return Output.debug(obj);
                var s:String = "";
                for (var i:String in this)
                {
                    s += "i: " + String( this[i] )+"\n";
                }
                return s;
            }

    }

}



Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions