0
(function (w, d, u) {
    /* Variable Conventions _*VAR*_ is html or class text*/
    var wl = '^\\/t\\d+',
    ps = '^\\/post',
    pv = '\\/privmsg\\?.+(post|reply){1}(.*)?',
    _GROUP_ = null,
    i,
    _CLASS_ = "sceditor-button sceditor-button-",
    _COMMAND_ = "data-sceditor-command",
    _TOOLBAR_ = "sceditor-toolbar",
    _MESSAGE_ = "message",
    _IFRAME_ = ".sceditor-container iframe",
    _BUTTON_ = "sceditor-button",
    _GROUPT_ = "sceditor-group",
    _TEXTAREA_ = "text_editor_textarea",
    options,
    interval,
    awe_debug = false;      

    var awe = function (opts) {
        var target = document.querySelector('#textarea_content');
        if (target) {
            var observer = new MutationObserver(function (mutations) {
                mutations.forEach(function (mutation) {
                    var added = mutation.addedNodes;
                    for (var i = 0; i < added.length; i++) {
                        if ((/sceditor-container/g).test(added[i].className)) {
                            return new AWE(opts);
                        } else easyDebug("info", "AW Editor::\n No mutation on object, so there was nothing to observe.");

                    }
                    observer.disconnect();
                });
            });
            var config = { addedNodes: true, attributes: true, childList: true, characterData: true };
            // pass in the target node, as well as the observer options
            observer.observe(target, config);
        } else return false;
    };

    var AWE = function (opts) {
        this.instantiate = false;
        this.debug = false;
        this.commandList = {};
        this.pathname = w.location.href.replace(w.location.origin, '');
        var runAt = [];
        if (opts && opts.location) {
            for (i = 0; i < 3; i++) {
                var loca = opts.location[i];
                switch (loca) {
                case 0:runAt.push(wl); break;
                case 1: runAt.push(ps); break;
                case 2: runAt.push(pv); break;
                case 3: runAt = [wl, ps, pv]; break;
                }
            }
        } else runAt = [wl, ps, pv];
        windowLocation = new RegExp(runAt.join("|"), 'gi');
        if (windowLocation.test(this.pathname)) {
            this.toolbar = getElement('class', _TOOLBAR_)[0];
            this.textarea = getElement('input', _MESSAGE_)[0];
            this.iframe = getElement('query', _IFRAME_)[0];
            this.instantiate = true;
            this.groups = getElement('class', _GROUPT_);
            var btns = getElement('class', _BUTTON_);
            for (i = 0; i < btns.length; i++) {
                this.commandList[btns[i].getAttribute(_COMMAND_)] = btns[i];
            }
            /*
             *
             *
             *  HERE IS THE ISSUE IT WON'T RETURN THIS!
             *  IF I DO return (window.extendAWE = extendAWE);
             *  It works but after a second
             */
            return this;
        } else easyDebug('info', "AW Editor::\n We couldn't instantiate AWE 3.4 please try again.");
    };

    awe.fn = AWE.prototype = {
        add: function (cmd, opts) {

        },
        remove: "",
        rearrange: "",
        id: "",
        destroy: "",
        insert: "",
        ajaxify: ""
    };  
    /*
     *  Next codes are functions to make awe faster and easier without using jQuery
     *  When editing ensure to use the debugger
     */
    function easyDebug(t, m) {
        if (awe_debug === true) return console[t](m);
    }
    return (window.editor = awe);
})(this, document);

上記の私のコードは、AWE関数ではこれを返しません! コードを連鎖の基本的なテンプレートに縮小しましたが、それは機能しますが、何らかの理由で元の状態に戻りません。私はすべてを試しましたが、今では面倒になりつつあります。それが私のスイッチケースなのだろうか?なぜ何も返さないのか、誰かが私に説明できますか?私は試しに試してみましたが、すぐ上return trueにできますが、何も返されませんconsole.log

var newEditor = editor({
    debug:true,
    defaultText:"How is going today?",
    removeWYSIWYG:true
});
//now that newEditor caches the returned object
newEditor.add('newButton',{
    //new button options
});

NEW オブザーバー反復

var awe = function (opts) {
    var target = document.querySelector('#textarea_content');

    if (target) {
        var observer = new MutationObserver(function(mutations) {
            var mutate = mutations[0].addedNodes;
            var ret;
            for(i=0;i<mutate.length;i++){
               if((/sceditor-container/g).test(mutate[i].className)){
               ret = new AWE(opts);
               break;
               }
            }
            return ret;
        });
        var config = {
            addedNodes: true,
            attributes: true,
            childList: true,
            characterData: true
        };
        // pass in the target node, as well as the observer options
        observer.observe(target, config);
    } else return false;
};
4

1 に答える 1

1

問題はここで発生しているようです:

mutations.forEach(function (mutation) {
    var added = mutation.addedNodes;
    for (var i = 0; i < added.length; i++) {
        if ((/sceditor-container/g).test(added[i].className)) {
            return new AWE(opts);
//          ^^^^^^^^^^^^^^^^^^^^^
        } else easyDebug("info", "AW Editor::\n No mutation on object, so there was nothing to observe.");

    }
    observer.disconnect();
});

コールバック内の戻り値.forEach()は無視されます。通常のループまたは外部の一時変数で修正されます。

于 2014-07-12T05:01:42.930 に答える