Something i needed for a recent project.
_global.SO = function(name) {
this.name = name;
};
// Method to create an object in your SO
SO.prototype.createObject = function(obj) {
var so = SharedObject.getLocal(this.name);
so.data[obj] = new Object();
so.flush();
};
// Method to delete an object in your SO
SO.prototype.deleteObject = function(obj) {
var so = SharedObject.getLocal(this.name);
delete so.data[obj];
};
// Method to display the objects in your SO
SO.prototype.showObjects = function() {
var so = SharedObject.getLocal(this.name);
trace("Objects inside "+this.name+":");
for (var prop in so.data) {
trace(prop);
}
};
// Method to set a property inside an object in your SO
SO.prototype.setObjectProperty = function(obj, prop, value) {
var so = SharedObject.getLocal(this.name);
so.data[obj][prop] = value;
so.flush();
};
// Method to get a property inside an object in your SO
SO.prototype.getObjectProperty = function(obj, prop) {
var so = SharedObject.getLocal(this.name);
return so.data[obj][prop];
};
// Method to display the properties inside an object in your SO
SO.prototype.showObjectProperties = function(obj) {
var so = SharedObject.getLocal(this.name);
trace("Props inside "+obj+":");
for (var prop in so.data[obj]) {
trace(prop+" = "+so.data[obj][prop]);
}
};
// Method to purge all objects inside an object in your SO
SO.prototype.purgeObjects = function() {
var so = SharedObject.getLocal(this.name);
for (var prop in so.data) {
delete so.data[prop];
}
};
//
// Usage:
//
// First create an instance of your SO class
so = new SO("Database");
// Create a new object inside your SO
so.createObject("user1");
// You can set properties to any object inside your SO
so.setObjectProperty("user1", "adress", "3th street");
so.setObjectProperty("user1", "tel", "555-5555");
// You get properties for any object inside your SO
trace("user1 adress:"+so.getObjectProperty("user1", "adress"));
// Print the objects inside your SO
so.showObjects();
// Check the properties of any object inside your SO
so.showObjectProperties("user1");
// Purge all objects inside your SO
so.purgeObjects();