-2

Before I start, I have already spent a long time reading this: How do I return the response from an asynchronous call?

I have a hard coded variable, called "d", defined as a 2d array:

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

I want to use an AJAX get so I can get the values from a database instead.

I tried this simple workaround:

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

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

But even if I do that, I'm still stuck because I still need to set the value of variable "d" to be the value returned by the "foo" function.

I need to use the code in the context of this code:

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"
    })
});

Where the var d = ... bit is replaced by what is returned by the AJAX call.

I can't rewrite the entire code as it's used by the TinyMCE plugin system.

I feel like I'm very close, but keep getting stuck and have been working on this on and off for a couple of days.

Community
  • 1
  • 1
4532066
  • 2,042
  • 5
  • 21
  • 48
  • 1
    No @idiot you can't return from asynchronous code. You have to either make request sync (which is deprectated on UI thread) or run your `a.addButton` code inside success callback. – Yury Tarabanko May 29 '16 at 18:27

1 Answers1

0

I got an answer in the end, using TimyMCE's own tinymce.util.XHR.send:

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

Answered here:

tinymce.util.XHR.send -> variable

More info via tinymce docs tinymce.util.XHR.send

4532066
  • 2,042
  • 5
  • 21
  • 48