Something i needed for a recent project.
// Constructor
_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, content) {
var so = SharedObject.getLocal(this.name);
so.data[obj][prop] = content;
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];
}
so.flush();
};
//
// Usage:
//
// First create an instance of your SO class
so = new SO("Database");
// Create an object inside your SO
so.createObject("user1");
so.createObject("user2");
// You can create properties to anuy object inside your SO
so.setObjectProperty("user1", "adress", "3th street");
so.setObjectProperty("user1", "tel", "555-5555");
so.setObjectProperty("user2", "adress", "5th street");
so.setObjectProperty("user2", "tel", "123-4567");
// Print the objects inside your SO
so.showObjects();
// Check the properties of any object inside your SO
so.showObjectProperties("user1");
so.showObjectProperties("user2");
// Purge all objects inside your SO
so.purgeObjects();