patternjavascriptMinor
A class for remember me
Viewed 0 times
rememberclassfor
Problem
Looking for feedback on correctness and code structure.
Follow up to:
SStorage (remember) - v0
Note that b.c. the user can only change the storage type when logging in, there is no need to copy back and forth between storage types.
```
/*****
**SStorage - handles remember option of logging in and namespaces the domain as well
*/
var SStorage = $A.Class.create({
Name: 'SStorage',
A: {
// storage type
storage: null,
// set the namespace here
ns: 'arcmarks_',
// storage key which keeps track of storage type
indicator: 'arcmarks_h_token'
},
// type of storage is set when the user logs in
// and called below
setType: function (remember) {
this.A.storage = remember ? $A.localStorage : $A.sessionStorage;
},
// determine type of storage at load MUserAny
load: function () {
this.setType($A.localStorage[this.A.indicator]);
},
// basic setters and getters with namespacing
set: function (key, value) {
this.A.storage[this.A.ns + key] = value;
},
get: function (key) {
return this.A.storage[this.A.ns + key];
},
setObj: function (o) {
$A.someKey(o, function (val, key) {
this.A.storage[this.A.ns + key] = val;
}, this);
},
getObj: function () {
var o = {},
real_key,
namespace;
$A.someKey(this.A.storage, function (val, key) {
// retrieves after the name-space
real_key = key.slice(this.A.ns.length);
// retrieves the name-space
namespace = key.slice(0, this.A.ns.length);
if (namespace === this.A.ns) {
o[real_key] =
Follow up to:
SStorage (remember) - v0
Note that b.c. the user can only change the storage type when logging in, there is no need to copy back and forth between storage types.
```
/*****
**SStorage - handles remember option of logging in and namespaces the domain as well
*/
var SStorage = $A.Class.create({
Name: 'SStorage',
A: {
// storage type
storage: null,
// set the namespace here
ns: 'arcmarks_',
// storage key which keeps track of storage type
indicator: 'arcmarks_h_token'
},
// type of storage is set when the user logs in
// and called below
setType: function (remember) {
this.A.storage = remember ? $A.localStorage : $A.sessionStorage;
},
// determine type of storage at load MUserAny
load: function () {
this.setType($A.localStorage[this.A.indicator]);
},
// basic setters and getters with namespacing
set: function (key, value) {
this.A.storage[this.A.ns + key] = value;
},
get: function (key) {
return this.A.storage[this.A.ns + key];
},
setObj: function (o) {
$A.someKey(o, function (val, key) {
this.A.storage[this.A.ns + key] = val;
}, this);
},
getObj: function () {
var o = {},
real_key,
namespace;
$A.someKey(this.A.storage, function (val, key) {
// retrieves after the name-space
real_key = key.slice(this.A.ns.length);
// retrieves the name-space
namespace = key.slice(0, this.A.ns.length);
if (namespace === this.A.ns) {
o[real_key] =
Solution
From a once over:
- You are a bit overdoing the blank lines in your definition of
A
- I like the namespace idea, given how you implement
getObjandsetObj, it would be nice if the caller could set the namespace.
setType-> I get thatrememberprobably means you want to remember info across sessions, hence you chooselocalStorageand notsessionStorage, still something likepersistorpersistAcrossSessionscould be more informative.
setObj-> does totally not what I would expect it to do, I would expect your code toJSON.stringify()and assign that to a key, however you store each property individually which can make for a very messy retrieval
getObj-> does exactly what I thought i would, give all properties in 1 object, this is very limited functionality. You should consider really consider JSON in your class.
clear-> I would makevar keypart of theforloop :for (var key in this.A.storage) {
- Commenting is uneven, the 2 functions which are most surprising to me have no comments.
Context
StackExchange Code Review Q#39854, answer score: 2
Revisions (0)
No revisions yet.