views:

190

answers:

1

I am (slowly) writing an XML parser for some "site definition" files that will drive a website. Many of the elements will be parsed in the same manner and I won't necessarily need to keep the values for each.

The XML

The parser so far

My question is actually pretty simple: How can I use jquery manipulators in an class function? How can I pass $(this)? I know that it sometimes refers to a DOM object and sometimes the jQuery object, but am a bit hazy.

For my function:

function parseXML(xml) {
 $("book, site", xml).children().each(function() {
  var label = $(this).get(0).tagName;
  var text = $(this).text();
  var key = toCamelCase(label);
  if ((key in siteData) && (text != -1)){
  if (isArray(siteData[key]))
  {   
   $(this).children().each(function (){
    var childLabel = $(this).get(0).tagName;
    var childText = $(this).text();
    var childKey = toCamelCase(childLabel);
    if(isArray(siteData[key][childKey]))
    {
      siteData[key][childKey].push(childText);  
    }
    else {
     siteData[key].push(childText);
    }
   });  
  }
  else 
  {
   siteData[key] = text;
   }
  };
 }); 
 }
 });

I want to place

var label = $(this).get(0).tagName; var text = $(this).text(); var key = toCamelCase(label);

in a class, so I can do something like

var child = new Element(); and var subchild = new Element();

and then use child.label , child.text and child.key... But again, not sure how to use the jquery methods with these... I have more nodes to process and I don't want to keep doing stuff like var label = $(this).get(0).tagName; and then var childLabel = $(this).get(0).tagName;

Thanks.

+1  A: 
var app = {};
app.element = function(data) {
    return function() {
    var _text = data.get(0).tagName, _label= data.text(), _key = toCamelCase(_label);
        var that = {};
        that.label = function() {
            return _label;
        }
        that.text = function() {
            return _text;
        }
        that.key = function() {
            return _key;
        }
        return that;
    }();
};
app.instanceA = app.element($(this));
document.writeln(app.instanceA.label());

Ok so this works but, I'm not sure if it's the best way.

Shawn Simon
oops, collision for .text()... gotta rename. other than that, it work, sure enough. also not sure if this is the best way.
two7s_clash
what you should do is get 'JavaScript: The Good Parts' its an awesome book and real short. I just finished it but there are a few chapters on inheritance. There is a better way to do this using prototype. I need to reread the book however ;p
Shawn Simon
i have an idea, lets make a question of this
Shawn Simon