如何在提交Tinymce内容之前等待ajax数据

时间:2017-04-25 10:55:49

标签: javascript jquery ajax tinymce

我需要在tinymce窗口中更新提交变量。

这里是tinymce代码:

tinymce.PluginManager.add('tc_button',function(editor,url){
    editor.addButton('tc_button',{
    title: 'Images',

    onclick: function(){
    editor.windowManager.open({
    title: 'Images',
    buttons: [{text: 'Add Shortcode', onclick: 'submit'}, {text: 'Cancel', onclick: 'close'}],

    body: [

    {type: 'container',
    label  : 'Image URL',
    items: [
    {type: 'textbox', id: 'image1', name : 'image1', style:'width:270px'},
    {type: 'listbox', id: 'effect1', name: 'effect1', style:'margin-left:10px'},
    ]}

    ],

    onsubmit: function(e) {


    jQuery.post(ajax_url, 
    {
    action: 'save_image',
    image: imageData,
    }
    ),

    function ( data ){
    var jsonData = tryParseJSON( data );
    if ( jsonData !== false ) {
    var new_url = jsonData.attachment_url;  
    jQuery('#image1').attr('src', new_url);     
    }
    };



    editor.insertContent('[shortcode image1="' + e.data.image1 + '"]' );


    }

    });

    }
    });
    });

我需要在编辑器中添加 jQuery('#image1').attr('src', new_url)之前在ajax调用内运行editor.insertContent。如何设置它以便等待ajax调用结束并使用ajax结果值更新#image1源?

提前致谢。

1 个答案:

答案 0 :(得分:0)

你需要在 jQuery之后调用 editor.insertContent('[shortcode image1 =“'+ e.data.image1 +'”]'); 里面的回调函数('# image1')。attr('src',new_url);

    tinymce.PluginManager.add('tc_button', function(editor, url) {
    editor.addButton('tc_button', {
        title: 'Images',

        onclick: function() {
            editor.windowManager.open({
                title: 'Images',
                buttons: [{
                    text: 'Add Shortcode',
                    onclick: 'submit'
                }, {
                    text: 'Cancel',
                    onclick: 'close'
                }],

                body: [

                    {
                        type: 'container',
                        label: 'Image URL',
                        items: [{
                                type: 'textbox',
                                id: 'image1',
                                name: 'image1',
                                style: 'width:270px'
                            },
                            {
                                type: 'listbox',
                                id: 'effect1',
                                name: 'effect1',
                                style: 'margin-left:10px'
                            },
                        ]
                    }

                ],

                onsubmit: function(e) {


                    jQuery.post(ajax_url, {
                            action: 'save_image',
                            image: imageData,
                        }),

                        function(data) {
                            var jsonData = tryParseJSON(data);
                            if (jsonData !== false) {
                                var new_url = jsonData.attachment_url;
                                jQuery('#image1').attr('src', new_url);
                            }
                            editor.insertContent('[shortcode image1="' + e.data.image1 + '"]');
                        };
                }

            });

        }
    });
});