在AJAX中重新初始化TinyMCE 4

时间:2015-02-25 21:50:50

标签: jquery ajax tinymce-4

我在启用了ajax的Foundation Reveal框中使用了TinyMCE。现在TinyMCE在第一次加载时踢,但是如果我关闭盒子然后再打开它就不会触发:(

我有其他脚本,如chosenmasked input在完全相同的前置查询中触发,但是当我第二次重新加载时,TinyMCE不会重新初始化

这是我目前的代码,我正按照this question的建议尝试:

$(document).on('opened.fndtn.reveal', '[data-reveal]', function () {
    $("#expenseUser, #expenseVendorID, #expenseCategoryID, #expenseAccidentID").chosen({disable_search_threshold: 5, width: '365px'});
    $("#expOdo").mask("999999",{placeholder:"0"});
    $('.datePicker').each(function() {
        $(this).datetimepicker({
            addSliderAccess: true,
            sliderAccessArgs: {touchonly: false},
            changeMonth: true,
            changeYear: true,
            altField: "#"+$(this).attr('id')+"Alt",
            altFieldTimeOnly: false,
            altFormat: "yy-mm-dd",
            altTimeFormat : "HH:mm:ss",
            dateFormat: "D, d MM yy",
            timeFormat: "hh:mmtt"
        });
    });
    tinymce.execCommand('mceRemoveEditor',true,"textarea#expenseComments");
    tinymce.execCommand('mceAddEditor',true,"textarea#expenseComments");
    tinyMCE.init({selector: "textarea#expenseComments",menubar : false,toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright"});
});

已更新

我已经尝试过改变以下运气,但我认为这是正确的道路?

$(document).on('opened.fndtn.reveal', '[data-reveal]', function () {
    $("#expenseUser, #expenseVendorID, #expenseCategoryID, #expenseAccidentID").chosen({disable_search_threshold: 5, width: '365px'});
    $("#expOdo").mask("999999",{placeholder:"0"});
    $('.datePicker').each(function() {
        $(this).datetimepicker({
            addSliderAccess: true,
            sliderAccessArgs: {touchonly: false},
            changeMonth: true,
            changeYear: true,
            altField: "#"+$(this).attr('id')+"Alt",
            altFieldTimeOnly: false,
            altFormat: "yy-mm-dd",
            altTimeFormat : "HH:mm:ss",
            dateFormat: "D, d MM yy",
            timeFormat: "hh:mmtt"
        });
    });     
    tinyMCE.init({selector: "textarea#expenseComments",menubar : false,toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright"});
}); 
$(document).on('close.fndtn.reveal', '[data-reveal]', function () {
    tinymce.execCommand('mceRemoveEditor',true,"textarea#expenseComments");
}); 

6 个答案:

答案 0 :(得分:5)

问题在于关闭盒子而不正确关闭内部tinymce实例将导致第二次不显示编辑器(因为变量window.tinymce.editors中仍然存在一个tinymce编辑器对象)。

解决方案:onclose(实际上在销毁或删除之前)关闭了编辑器。

答案 1 :(得分:5)

删除现有的tinymce编辑器并添加tinymce.EditorManager.editors数组的新需求清除。 此解决方案适用于两种情况:

  1. 如果您只有一个编辑器,并且想要删除并重新添加它。
  2. 如果您有多个编辑器,并且想要删除一些特殊编辑器并再次添加它。

    的console.log(tinymce.EditorManager.editors);

  3. 这将为您提供要删除的所需编辑器的数组和精确索引的视图。例如,上述控制台的一个示例输出可以是:

    Array[2]
    0:B
    1:B
    length:2
    textarea-1:B
    textarea-2:B
    _proto_Array[0]
    

    当我在textareas上有两个tinymce编辑器时,这是控制台的输出:#textarea-1和#textarea-2 让我们假设我想删除#textarea-2并重新添加它然后可以按如下方式完成:

    tinymce.EditorManager.editors.splice(1, 1);//removing second element in array.
    delete tinymce.EditorManager.editors['textarea-2'];//deleting respective textarea id from array
    

    然后你可以使用init:

    再次添加它
    tinymce.init({
        selector:'#ts-textarea-2'
    });
    

    如果你只有一个textarea关联tinymce编辑器,请说:#textarea-1,你想删除并重新初始化它然后你可以只是空 tinymce.EditorManager.editors:

    tinymce.EditorManager.editors = [];
    

    然后您可以使用init命令添加,如上所述。为我工作没有任何错误。

    我希望它有所帮助

答案 2 :(得分:0)

这似乎是一个时间问题。我正在寻找一个事件监听器类型的东西,而不是一个计时器,但当我包装我的初始化函数,它正常工作。我在这里发现了这篇文章的提示:http://www.tinymce.com/forum/viewtopic.php?id=32661

tinyMCE.execCommand('mceRemoveEditor', false, 'editorId');
setTimeout(function() {
    tinyMCE.init(myconfig);
}, 3000);

答案 3 :(得分:0)

需要清除旧的tinymce实例。以下代码为我工作

tinymce.EditorManager.editors = []; //我们需要删除旧实例。

//设置"关于"部分到TinyMCE编辑器。 tinymce.init({   选择器:' textarea',   skin_url:' / packages / teamon_tinymce / skins / lightgray',   菜单栏:假,   工具栏:'插入|粗体斜体| alignleft aligncenter alignright alignjustify | bullist numlist', }); tinymce.get(' pageContent&#39)。setContent(page.content);

答案 4 :(得分:0)

希望这将是解决此问题的更完整指南。



console.log("before remove")
//capture the editorId from this (id: )
console.log(tinymce.EditorManager.editors);
//remove instance 
tinymce.execCommand('mceRemoveEditor', true, "editorId");
//add to the DOM the html to whom you will apply tinymce
$("#emailEditor").append(html);
console.log("before add")
console.log(tinymce.EditorManager.editors);
//add instance
tinymce.EditorManager.execCommand('mceAddEditor', false, "editorId");
//init tinymce
initTinyMce();

//be sure to add the init_instance_callback part
function initTinyMce() {
    console.log("init tinymce")
    tinymce.init({
        //script_url: '~/Scripts/tinymce/jquery.tiny_mce.js',
        selector: "textarea#editorId",
        // General options
        mode: "textareas",
        theme: "modern",
        // Theme options
        theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,
        init_instance_callback: function (editor) {
            var currentEditor = editor.editorContainer;
            $(currentEditor).show();
        }
    });
}




答案 5 :(得分:-1)

这解决了我的问题:

// Remove all editors bound to divs

tinymce.remove('div');

// Remove all editors bound to textareas
tinymce.remove('textarea');

// Remove all editors
tinymce.remove();

// Remove specific instance by id
tinymce.remove('#id');

https://www.tiny.cloud/docs/api/tinymce/root_tinymce/?_ga=2.143898737.597721600.1600176559-1290517464.1591019990#remove