By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

getting property of a wrapper object

Dormilich
Expert Mod 5K+
P: 8,639
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
Oct 11 '09 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
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
Oct 12 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
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.
Oct 12 '09 #3

gits
Expert Mod 5K+
P: 5,390
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
Oct 12 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
I guess Iíll use a global, because I donít consider the effort worth the gain.
Oct 12 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.