从异步调用

时间:2016-05-29 18:19:20

标签: javascript ajax asynchronous

在开始之前,我已经花了很长时间阅读这篇文章: How do I return the response from an asynchronous call?

我有一个名为“d”的硬编码变量,定义为2d数组:

var d = [
    ["05_001","05_002","05_003","05_004","05_005"],
    ["05_006","05_007","05_008","05_009","05_010"]
];

我想使用AJAX get,所以我可以从数据库中获取值。

我尝试了这个简单的解决方法:

function foo(callback) {
    return $.ajax({
        url: 'emoji.php?cat=emoticons_activity',
        success: callback
    });
}

var result;
foo(function(response) {
    result = response;
    alert(result);
});

但即使我这样做,我仍然卡住了,因为我仍然需要将变量“d”的值设置为“foo”函数返回的值。

我需要在此代码的上下文中使用代码:

tinymce.PluginManager.add("emoticons_travel_places", function(a, b) {
    function c() {
        var a;
        return a = '<table role="list" class="mce-grid">', tinymce.each(d, function(c) {
            a += "<tr>", tinymce.each(c, function(c) {
                var d = b + "/img/" + c + ".svg";
                a += '<td><a href="#" data-mce-url="' + d + '" data-mce-alt="' + c + '" tabindex="-1" role="option" aria-label="' + c + '"><img src="' + d + '" style="width:30px; height:30px" role="presentation" /></a></td>'
            }), a += "</tr>"
        }), a += "</table>"
    }

    var d = [
        ["05_001","05_002","05_003","05_004","05_005","05_006","05_007","05_008","05_009","05_010","05_011","05_012","05_013","05_014","05_015","05_016","05_017","05_018","05_019","05_020"],
        ["05_021","05_022","05_023","05_024","05_025","05_026","05_027","05_028","05_029","05_030","05_031","05_032","05_033","05_034","05_035","05_036","05_037","05_038","05_039","05_040"],
        ["05_041","05_042","05_043","05_044","05_045","05_046","05_047","05_048","05_049","05_050","05_051","05_052","05_053","05_054","05_055","05_056","05_057","05_058","05_059","05_060"],
        ["05_061","05_062","05_063","05_064","05_065","05_066","05_067","05_068","05_069","05_070","05_071","05_072","05_073","05_074","05_075","05_076","05_077","05_078","05_079","05_080"],
        ["05_081","05_082","05_083","05_084","05_085","05_086","05_087","05_088","05_089","05_090","05_091","05_092","05_093","05_094","05_095","05_096","05_097","05_098","05_099","05_100"],
        ["05_101","05_102","05_103","05_104","05_105","05_106","05_107","05_108","05_109","05_110","05_111","05_112","05_113","05_114","05_115"]
    ];

    a.addButton("emoticons_travel_places", {
        type: "panelbutton",
        panel: {
            role: "application",
            autohide: !0,
            html: c,
            onclick: function(b) {
                var c = a.dom.getParent(b.target, "a");
                c && (a.insertContent('<img src="' + c.getAttribute("data-mce-url") + '" width="30" height="30" alt="' + c.getAttribute("data-mce-alt") + '" />'), this.hide())
            }
        },
        tooltip: "Emoticons - Travel & Places"
    })
});

var d = ...位替换为AJAX调用返回的内容。

我无法重写TinyMCE插件系统使用的整个代码。

我觉得自己非常接近,但是一直坚持不懈,并且已经开始和关闭了几天。

1 个答案:

答案 0 :(得分:0)

我最后得到了一个答案,使用了TimyMCE自己的tinymce.util.XHR.send

tinymce.util.XHR.send({
    url: 'emoji.php?cat=emoticons_travel_places',
    success: function(returnedData) {
        MyData = JSON.parse(returnedData);
        d = MyData;
       }
});

在这里回答:

tinymce.util.XHR.send -> variable

更多信息来自tinymce docs tinymce.util.XHR.send