getting property of a wrapper object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    getting property of a wrapper object

    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:
    Code:
    // some stuff left out
    function WDDX(xmldoc)
    {
    	var data = xmldoc.getElementsByTagName("data")[0];
    	this.content = [B]new WDDXNode(data.getFirstElementChild())[/B];
    	[U]this.setting = null;[/U]
    }
    
    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 ([U]setting[/U] == "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 = [B]new WDDXNode(items[i].getFirstElementChild())[/B];
    		StructIndex = items[i].getAttribute("name");
    		JSObject[StructIndex] = item.parse();
    	}
    	return JSObject;
    }
    the usage is quite plain:
    Code:
    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
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

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

    Code:
    function WDDX(xmldoc) {
        var data = xmldoc.getElementsByTagName("data")[0];
        this.content = new WDDXNode(
            data.getFirstElementChild(),
            this
        );
        this.setting = null;
    }
    and:
    Code:
    function WDDXNode(knoten, parent) {
        this.node = knoten;
        this.name = knoten.tagName.toLowerCase();
        this.text = knoten.firstChild.data;
        this.elements = knoten.getElementChildren();
    
        this.parentSetting = function() {
            return parent.setting;
        }
    }
    kind regards

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

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

          Comment

          Working...