How do I create a jQuery plugin extending $.Deferred?

523 views Asked by At

I want to extend $.Deferred with a new method which is supposed to have a signature similar to Deferred.fail(). What is the best way of doing that?

1

There are 1 answers

1
Shalom Aleichem On BEST ANSWER

Found a way to do it.

I'm overriding $.Deferred with a new function that calls the base function and extends the object it returns with a new method:

$._Deferred = $.Deferred;
$.Deferred = function (param) {
    var deferredToExtend = $._Deferred(param);

    deferredToExtend.test = function (param) {
        var deferred = $.Deferred();
        deferredToExtend.always(deferred.resolve.bind(deferred, param));
        return deferred.promise();
    };

    return deferredToExtend;
};

Here is the jsfiddle