mayjax icon indicating copy to clipboard operation
mayjax copied to clipboard

Issue with TrafficCop 0.3.0 with jQuery 3.1.1

Open bzamfir opened this issue 8 years ago • 1 comments

Hi,

I know this is related to a pretty old version, but I'd like to ask if you can assist with making old TrafficCop 0.3.0 compatible with jquery 3.1.1

I use TrafficCop as dependency for infuser 0.2.1, which in turn is used by a module which implements external templates for knockout.js (used together with KendoUI and ko-kendo module).

Everything works ok with jQuery version shipped with kendoUI (1.12 currently)

However I was trying to see if we can move on to latest jQuery (3.1.1 / 3.1.2) and the first stuck was with TrafficCop.

The code I have is

var inProgress = {};

$.trafficCop = function(url, options) {
    var reqOptions = url, key;
    if(arguments.length === 2) {
        reqOptions = $.extend(true, options, { url: url });
    }
    key = JSON.stringify(reqOptions);
    if (key in inProgress) {
        for (i in {success: 1, error: 1, complete: 1}) {
            inProgress[key][i](reqOptions[i]);
        }
    } else {
        inProgress[key] = $.ajax(reqOptions).always(function () { delete inProgress[key]; });
    }
    return inProgress[key];
};

})(jQuery);

and the problem is in the line

inProgress[key][i](reqOptions[i]);

Error: inProgress[key][i](reqOptions[i]); is not a function

Can you figure out what is the difference between $ajax in jquery 1.x and 3.1.x that cause this error?

I appreciate any suggestion.

Thank you

bzamfir avatar Sep 16 '17 23:09 bzamfir

I investigated the issue further and I figure out the problem: starting with jQuery 3, the methods success, error and complete of the jqXHR object were replaced with done, fail, always, which broke the TrafficCop code.

I fixed it with the following code

//....
    if (key in inProgress) {
        var callback = {success: 'done', error: 'fail', complete: 'always'};
        for (var i in callback) {
            var reqCbk = i,
                ajaxCbk = i;
            
            if (jQuery.fn.jquery.substring(0, 1) >= '3')
                ajaxCbk = callback[i];

            inProgress[key][ajaxCbk](reqOptions[reqCbk]);
        }
    } else {
//....

I attach also the SVN patch (I'm using svn on daily basis and I'm not very familiar with git, to submit a pull request).

It would be great if you could release a new TrafficCop nuget package with this fix, to make it compatible with jQuery 3.X

Cordially Bogdan TrafficCop.patch.zip

bzamfir avatar Sep 16 '17 23:09 bzamfir