开发者

a basic javascript class and instance using jquery "$(this)" for XML parser

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 !=开发者_JAVA技巧 -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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜