Pergunta

Estou (lentamente) escrevendo um analisador XML para alguns arquivos de "definição de site" que irão conduzir um site.Muitos dos elementos serão analisados ​​da mesma maneira e não precisarei necessariamente manter os valores de cada um.

O XML

O analisador até agora

Minha pergunta é realmente muito simples:Como posso usar manipuladores jquery em uma função de classe?Como posso passar $(this)?Eu sei que às vezes se refere a um objeto DOM e às vezes ao objeto jQuery, mas estou um pouco confuso.

Para minha função:

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;
            }
        };
    }); 
    }
    });

eu quero colocar

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

em uma aula, para que eu possa fazer algo como

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

e então use child.label , child.text and child.key...Mas, novamente, não tenho certeza de como usar os métodos jquery com estes...Tenho mais nós para processar e não quero continuar fazendo coisas como var label = $(this).get(0).tagName; and then var childLabel = $(this).get(0).tagName;

Obrigado.

Foi útil?

Solução

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, isso funciona, mas não tenho certeza se é a melhor maneira.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top