0
\$\begingroup\$

This basically creates a namespace for Storage among other things. I'm looking for feedback on the structure of the code.

/******************************************************************************************************************(120)
** Storage
** - Dependencies - None
** - Basically creates a namespace for Storage facilities
** - Used to manage local and session storage for login purposes
** - Note - underscore each() will error on web storage because of the length property
*/

    var Storage = $A.Obj.create('public', {
        Name: 'Storage',
        S: {

            // will point to localStorage or sessionStorage
            storage:     null,

            // namespace for this class - note all librarys use the same web storage
            ns:          'arc_',

            // check this identifier in local storage to see if a user is logged in
            identifier:  'arc_h_token'
        },
        init: function () {

            // always use localStorage for now - removed checkbox
            this.S.storage = localStorage;

            // if the token is in localStorage a user is already logged in permanently
            // this.S.storage = localStorage[this.S.identifier] ? localStorage : sessionStorage;
        },
        setToken: function (value) {
            localStorage[this.S.identifier] = value;
        },

        // Utility Fucntions below
        // clear all namespace properties
        clear: function () {
            $A.someKey(this.S.storage, function (val, key) {
                if (key.slice(0, this.S.ns.length) === this.S.ns) {
                    this.S.storage.removeItem(key);
                }
            }, this);
        },
        set: function (key, value) {
            this.S.storage[this.S.ns + key] = value;
        },
        get: function (key) {
            return this.S.storage[this.S.ns + key];
        },
        setObj: function (o) {
            $A.someKey(o, function (val, key) {
                this.S.storage[this.S.ns + key] = val;
            }, this);
        },
        getObj: function () {
            var o = {};
            $A.someKey(this.S.storage, function (val, key) {
                if (key.slice(0, this.S.ns.length) === this.S.ns) {
                    o[key.slice(this.S.ns.length)] = val;
                }
            }, this);
            return o;
        }
    });
\$\endgroup\$
1
\$\begingroup\$

S, ns and identifier could be a bit more descriptive (although S also seems to add no value anyway and its properties could easily be moved to the main object).

storage doesn't seem to serve a real purpose.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.