JavaScript捕获的事件监听器和MutationObserver

时间:2014-06-14 19:41:29

标签: javascript mutation-observers

所以我试图改变我的一些JavaScript

从此

content.forumobserver = content.build.forumobserver = function() {
    var target = document.querySelector('#content-padding');
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function(mutation, idx, mutated) {
            if(mutation.addedNodes.length === 21) {
                for(var i = 0; i < mutation.addedNodes.length; i++) {
                    if(mutation.addedNodes[i].id === 'post_container') {
                        // update
                        content.c_page = document.querySelector('.page_current');
                        content.t_url = content.c_page.href;
                        content.t_page = content.t_url.replace(content.regex.url, '$3');
                        // reload
                        content.init(options);
                        // page is loaded
                        observer.disconnect();
                    };
                };
            };
        });
    });
    $.each(document.querySelectorAll('.yui3-pjax'), function(idx, pjax) {
       pjax.addEventListener('click', function() {
           observer.observe(target, {
               childList: true
           });
       });
    });
};

到此

content.forumobserver = content.build.forumobserver = function() {
    var target = document.querySelector('#content-padding');
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function(mutation, idx, mutated) {
            if(mutation.addedNodes.length === 21) {
                for(var i = 0; i < mutation.addedNodes.length; i++) {
                    if(mutation.addedNodes[i].id === 'post_container') {
                        // update
                        content.c_page = document.querySelector('.page_current');
                        content.t_url = content.c_page.href;
                        content.t_page = content.t_url.replace(content.regex.url, '$3');
                        // reload
                        content.init(prefs);
                        // page is loaded
                        observer.disconnect();
                    };
                };
            };
        });
    });
    document.body.addEventListener('click', function(evt) {
        if(evt.target.classList.contains('yui3-pjax')) {
            observer.observe(target, {
                childList: true
            });
        };
    }, true);
};

唯一的问题是两者中的后者,当观察者到达我想要的点时,观察者不会断开连接。我在content.init()函数中调用content.forumobserver()。我知道它没有断开连接而不是向主体添加多个事件监听器的原因是因为如果我添加一个if语句来检查初始化值,那么调用content.forumobserver()并将initialized设置为true在content.init()函数中,MutationObserver仍然重新添加所有内容,但是对于加载的每个附加页面,添加的内容加倍,1,2,4,8等。

那么如何在内容从捕获的事件加载后断开MutationObserver,而不必将click事件显式绑定到yui3-pjax锚点?

1 个答案:

答案 0 :(得分:1)

使用

解决了这个问题
content.forumobserver = content.build.forumobserver = function(evt) {
    if(evt.target.classList.contains('yui3-pjax') || evt.target.parentNode.classList.contains('yui3-pjax')) {
        var target = document.querySelector('#content-padding');
        var observer = new MutationObserver(function (mutations) {
            mutations.forEach(function(mutation, idx, mutated) {
                if(mutation.addedNodes.length === 21) {
                    for(var i = 0; i < mutation.addedNodes.length; i++) {
                        if(mutation.addedNodes[i].id === 'post_container') {
                            // update
                            content.c_page = document.querySelector('.page_current');
                            content.t_url = content.c_page.href;
                            content.t_page = content.t_url.replace(content.regex.url, '$3');
                            // reload
                            content.init(options);
                            // page is loaded
                            observer.disconnect();
                        };
                    };
                };
            });
        });
        observer.observe(target, {
            childList: true
        });
    };
};

document.body.addEventListener('click', content.forumobserver, true);