在AJAX JSF请求之后重新加载Javascript

时间:2015-03-22 14:03:56

标签: javascript ajax jsf jsf-2

好吧,我正在使用TinyMCE编辑器,当我尝试打开模态对话框时,tinyMCE没有被渲染。

查找

$(document).ready(function() {

    jQuery('.tinymce').on('show',function(e){
        initTinymce();
    });

});

function initTinymce(){
    tinymce.init({
        selector: ".tinymce",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
    }); 
}

我的页面:

<div class="form-group">
                                <label>Conteúdo *</label>
                                <h:inputTextarea value="#{cursoMB.paginaConteudo.conteudo}"
                                    id="conteudo" required="true"
                                    requiredMessage="O conteúdo é obrigatório"
                                    styleClass="form-control tinymce">
                                </h:inputTextarea>
                            </div>

我的ajax电话:

<h:commandButton styleClass="btn btn-default" type="button"
                            value="Criar Página"
                            actionListener="#{cursoMB.novaPaginaConteudo()}">
                            <f:passThroughAttribute name="data-toggle" value="modal" />
                            <f:passThroughAttribute name="data-target"
                                value="#modalDialogPagina" />
                            <f:ajax execute="@this" render="modalPagina" />
                        </h:commandButton>

我该如何解决这个问题?

编辑1

我尝试使用jsf.ajax.addOnEvent但也没有工作,tinyMCE没有渲染(console.log显示正常):

$(document).ready(function() {

    initTinymce();
    jsf.ajax.addOnEvent(ajaxHandleTinymce);
});

function ajaxHandleTinymce(data){
    console.log('ajaxHandle');
    initTinymce();
}

function initTinymce(){
    tinymce.init({
        selector: ".tinymce",
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
    }); 
}

1 个答案:

答案 0 :(得分:1)

你必须在ajax调用之后初始化你的小部件,你可以用这种方式在f:ajax附加一个javascript事件监听器:

<f:ajax execute="@this" render="modalPagina" onevent="handleAjaxResponse"/>

function handleAjaxResponse(data) {
   if (data.status === 'success') {
      initTinymce();
   }
}

我建议不要为你的案例使用jsf.ajax.addOnEvent回调,因为每次向服务器发出ajax调用时,widget都将是init。