Pagedown编辑器附加处理程序到图像对话框

时间:2016-01-03 21:39:24

标签: javascript asynchronous callback handler pagedown

我已经使用语义用户界面modal建立了自己的上传库对话框。我试图将此对话框集成到Pagedown editor

var converter = Markdown.getSanitizingConverter();
var editor = new Markdown.Editor(converter);

editor.hooks.set("insertImageDialog", function(callback) {
    uploadModal.load(); // The dialog HTML gets loaded asynchronously

    $('body').on('selection', '.upload-modal', function(e, src) {
        console.log(src);
        callback(src);
    });

    return true; // tell the editor that we'll take care of getting the image url
});

第一次从对话框中选择图像时,一切正常,但之后就会中断:

  

未捕获的TypeError:无法调用方法' removeChild' of null Markdown.Editor.js

根据this SO question,它与多次定义事件处理程序有关,但我无法绕过它。

更多信息:

用于初始化模态的HTML是异步加载的,这里是JS代码:

var uploadModal = (function() {

    /**
     * Load the upload modal.
     */
    this.load = function() {
        var $modal = $('.upload-modal');

        if ($modal.length) {
            // Show the already existing modal.
            $modal.modal('show');
        } else {
            // Get the HTML
            var request = $.get(appUrl('admin/upload/modal'));

            request.done(function(html) {
                // Make the modal.
                var $modal = $(html);

                $modal.modal({
                    observeChanges: true,
                    onApprove: function () {
                        var upload = $modal.find('.selected.upload'),
                            src = upload.find('.image > img').attr('src');

                        // Bind the event listener which will return the upload's source URL
                        $modal.trigger('selection', src);

                        return true;
                    }
                }).modal('show');

                // Some event handlers here    
            });
        }
    };
})();

我通过调用uploadModal.load()来加载模态,并通过附加'选择'来观察何时选择图像。事件处理程序。

0 个答案:

没有答案