Connecting Tech Pros Worldwide Help | Site Map

getting property of a wrapper object

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#1: Oct 11 '09
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:
Expand|Select|Wrap|Line Numbers
  1. // some stuff left out
  2. function WDDX(xmldoc)
  3. {
  4.     var data = xmldoc.getElementsByTagName("data")[0];
  5.     this.content = new WDDXNode(data.getFirstElementChild());
  6.     this.setting = null;
  7. }
  8.  
  9. WDDX.prototype.deserialize = function()
  10. {
  11.     return this.content.parse();
  12. }
  13.  
  14. function WDDXNode(knoten)
  15. {
  16.     this.node = knoten;
  17.     this.name = knoten.tagName.toLowerCase();
  18.     this.text = knoten.firstChild.data;
  19.     this.elements = knoten.getElementChildren();
  20. }
  21.  
  22. WDDXNode.prototype.parse = function()
  23. {
  24.     switch (this.name) {
  25.         case "array"   : return this.getArray();
  26.         case "boolean" : return this.getBoolean();
  27.         // and some more
  28.     }
  29. }
  30.  
  31. // a method where new objects are created
  32. WDDXNode.prototype.getStruct = function()
  33. {
  34.     var JSObject, StructIndex, items;
  35.     var type = String(this.node.getAttribute("type"));
  36.     if (setting == "whatever value")
  37.         // do some additional stuff like getting the classname from elsewhere
  38.     if (type && "function" == typeof window[type])
  39.         JSObject = new window[type];
  40.     else
  41.         JSObject = new Object;
  42.  
  43.     items = this.node.getElementChildren("var");
  44.     for (var l, i = 0, l = items.length; i < l; i++) { 
  45.         var item = new WDDXNode(items[i].getFirstElementChild());
  46.         StructIndex = items[i].getAttribute("name");
  47.         JSObject[StructIndex] = item.parse();
  48.     }
  49.     return JSObject;
  50. }
the usage is quite plain:
Expand|Select|Wrap|Line Numbers
  1. var x = new WDDX(XMLHttpRequest.responseXML);
  2. x.setting = "whatever value";
  3. 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
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,126
#2: Oct 12 '09

re: getting property of a wrapper object


why not passing the 'parent'-obj reference to the child-constructor like:

Expand|Select|Wrap|Line Numbers
  1. function WDDX(xmldoc) {
  2.     var data = xmldoc.getElementsByTagName("data")[0];
  3.     this.content = new WDDXNode(
  4.         data.getFirstElementChild(),
  5.         this
  6.     );
  7.     this.setting = null;
  8. }
and:
Expand|Select|Wrap|Line Numbers
  1. function WDDXNode(knoten, parent) {
  2.     this.node = knoten;
  3.     this.name = knoten.tagName.toLowerCase();
  4.     this.text = knoten.firstChild.data;
  5.     this.elements = knoten.getElementChildren();
  6.  
  7.     this.parentSetting = function() {
  8.         return parent.setting;
  9.     }
  10. }
  11.  
kind regards
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#3: Oct 12 '09

re: getting property of a wrapper object


I hoped, I could do without passing the wrapper object… and I probably don’t need the closure since the WDDXNode cascade will be started only by parsing the first node.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,126
#4: Oct 12 '09

re: getting property of a wrapper object


while you don't need the closure (it was just an example :) ) i think without passing the reference it would get quite complicated to get the reference then? you could even try to use a setter for the 'setting' property that passes something (or notify) the child node when the property is changing.

kind regards
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: Oct 12 '09

re: getting property of a wrapper object


I guess I’ll use a global, because I don’t consider the effort worth the gain.
Reply