473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getting property of a wrapper object

Dormilich
8,658 Expert Mod 8TB
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
4 1783
gits
5,390 Expert Mod 4TB
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
8,658 Expert Mod 8TB
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
5,390 Expert Mod 4TB
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
8,658 Expert Mod 8TB
I guess I’ll use a global, because I don’t consider the effort worth the gain.
Oct 12 '09 #5

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

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
31
by: Thanos Tsouanas | last post by:
Hello. I would like to have a quick way to create dicts from object, so that a call to foo would return obj.bar. The following works, but I would prefer to use a built-in way if one exists. ...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
7
by: siddhiash | last post by:
Hi Friends I want to add PasswordChar Property which shows ****** for string which I type in PropertyGrid Control. Regards, Siddharth
3
by: jack bretcher | last post by:
I am developing a DDL that will be a wrapper for an existing set of libs. What I am trying to do is implement the class.error property in my wrapper. I have tried to create a readonly property...
8
by: Richard Collette | last post by:
When attempting to debug a webservice, I get the error: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary. In reading...
7
by: pooba53 | last post by:
I am working with VB .NET 2003. Let's say my main form is called Form1. I have to launch a new form (Form2) that gathers input from the user. How can I pass variable information back to Form1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.