Hi,
I’m doing XML deserialization and I want the properties of a wrapper object accessible to all subobjects, that are created inside it.
first some code to show how it works:
- // some stuff left out
-
function WDDX(xmldoc)
-
{
-
var data = xmldoc.getElementsByTagName("data")[0];
-
this.content = new WDDXNode(data.getFirstElementChild());
-
this.setting = null;
-
}
-
-
WDDX.prototype.deserialize = function()
-
{
-
return this.content.parse();
-
}
-
-
function WDDXNode(knoten)
-
{
-
this.node = knoten;
-
this.name = knoten.tagName.toLowerCase();
-
this.text = knoten.firstChild.data;
-
this.elements = knoten.getElementChildren();
-
}
-
-
WDDXNode.prototype.parse = function()
-
{
-
switch (this.name) {
-
case "array" : return this.getArray();
-
case "boolean" : return this.getBoolean();
-
// and some more
-
}
-
}
-
-
// a method where new objects are created
-
WDDXNode.prototype.getStruct = function()
-
{
-
var JSObject, StructIndex, items;
-
var type = String(this.node.getAttribute("type"));
-
if (setting == "whatever value")
-
// do some additional stuff like getting the classname from elsewhere
-
if (type && "function" == typeof window[type])
-
JSObject = new window[type];
-
else
-
JSObject = new Object;
-
-
items = this.node.getElementChildren("var");
-
for (var l, i = 0, l = items.length; i < l; i++) {
-
var item = new WDDXNode(items[i].getFirstElementChild());
-
StructIndex = items[i].getAttribute("name");
-
JSObject[StructIndex] = item.parse();
-
}
-
return JSObject;
-
}
the usage is quite plain:
- var x = new WDDX(XMLHttpRequest.responseXML);
-
x.setting = "whatever value";
-
var deser = x.deserialize();
the
setting property is a value, that should be used in some of the WDDXNode methods, but I don’t have an idea, how to pass it around. I thought of the Observer Pattern, but I can’t pass around the WDDX object as Subject (if I do that, I could read the value directly…) and I don’t want to use a global, since it would affect all active WDDX instances. so has anyone an idea how to do that?
thanks, Dormi