jsonp

webix.jsonp(url, params, callback, master)

webix.jsonp helper.

Please look into the linked official documentation.

References

helpers
timer(), uid().

External references

Official documentation page.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
webix.jsonp = function(url, params, callback, master){
    var defer = webix.promise.defer();

    var id = "webix_jsonp_"+webix.uid();
    var script = document.createElement('script');
    script.id = id;
    script.type = 'text/javascript';

    var head = document.getElementsByTagName("head")[0];

    if (typeof params == "function"){
        master = callback;
        callback = params;
        params = {};
    }

    if (!params)
        params = {};

    params.jsonp = "webix.jsonp."+id;
    webix.jsonp[id]=function(){
        if (callback)
            callback.apply(master||window, arguments);
        defer.resolve(arguments[0]);

        window.clearTimeout(timers[id]);
        delete timers[id];

        script.parentNode.removeChild(script);
        callback = head = master = script = null;
        delete webix.jsonp[id];
    };

    //timeout timer
    timers[id] = window.setTimeout(function(){
        defer.reject();
        delete webix.jsonp[id];
    }, webix.jsonp.timer);

    var vals = [];
    for (var key in params) vals.push(key+"="+encodeURIComponent(params[key]));

    url += (url.indexOf("?") == -1 ? "?" : "&")+vals.join("&");

    script.src = url;
    head.appendChild(script);

    return defer;
};