Best Practices

Search:

Welcome

Login to contribute. Register if you haven't yet.

Need Support?

Search this site to find an answer. If you still need help, post a Question. Private or account-related support inquiries can be made at the Private Support forum.

Use Object Serialization (the O2X library)

In EditMe's custom development projects, we frequently use a library we call O2X to serialize simple JavaScript objects into compact XML for storage in a Data entry. It creates an object called "o2x" that can be used to serialize and unserialize JavaScript objects. See the source code for a full method listing.

Just put the library code into a page called O2X with MIME Type set to "text/vnd.editme.js" and the "hidden" box checked. Get the code here.

Note that this class only handles Navive JavaScript types (number, string, boolean, Date, Array, Object). If you try to serialize an EditMe Page object, for example, it won't work as this isn't a native JavaScript class. 

Here's a usage scenario:

<div>
<script editme="if this.O2X==undefined ;; process 'O2X' ;; text ' ' "></script>
<script type="text/vnd.editme.js"><![CDATA[
// class to define simple JavaScript object structure - this is a template for new objects
function Person(name, age) {
this.name = name;
this.age = age;
this.children = new Array();
this.created = new Date();
}

// create an instance of the class var obj = new Person('Fred Wilson', 42);
obj.children.push(new Person('Susy Wilson', 16)); obj.children.push(new Person('Charlotte Wilson', 14));
// serialize the object to an XML string var xml = o2x.objectToXml(obj);
// save in a data entry, pass through a form field, or do whatever...
// read the xml back into an object var obj2 = o2x.xmlToObject(xml); obj2.children.length == 2; // true
// read and save methods combine these with data.get() and data.save()...   // reads the 'mykey' object or returns a new instance of Person var obj3 = o2x.readObject('mykey', Person);
// reads the 'mykey' object or returns a new instance of Person o2x.saveObject('mykey', obj3);
]]></script> </div>