Jquery - Context of ajax callbacks not adhering to supplied context argument
<p>I have a jquery plugin which I use to wrap all my ajax calls. It's intended to do some generic pre/post processing of input/output data.</p>

<pre><code>(function ($) {
    $.ajaxCall = function () {

        var myCall = this;
        myCall.settings = {};

        myCall.ExecuteService = function (caller) {
            var ajax = $.ajax({
                type: 'POST',
                url: '../myWebservice',
                dataType: "json",
                context: caller,
                success: myCall.settings.onSuccess
            });
        };
    };
} (jQuery));
</code></pre>

<p>My problem is when I try to modify the onSuccess event to be more than a passed in function. I have the following code which makes use of the the plugin.</p>

<pre><code>this.foo = 'fooStr';
function myOnSuccess(result) {
    alert(this.foo);
}

var newCall = new $.ajaxCall();
newCall.settings.onSuccess = myOnSuccess;
newCall.ExecuteService(this);

function myProto() {
    this.foo = 'otherFooStr';
}

myProto.prototype.success = function(result){
    alert(this.foo);
}

myProto.prototype.makeCall = function(){
    var newCall = new $.ajaxCall();
    newCall.settings.onSuccess = this.success;
    newCall.ExecuteService(this);
}

var proto = new myProto();
proto.makeCall();
</code></pre>

<p>This displays 'fooStr' &amp; 'otherFooStr', and appears to be working fine. But if I try to modify my plugin to do more in the success callback, I have context issues.</p>

<pre><code>myCall.ExecuteService = function (caller) {
    var ajax = $.ajax({
        type: 'POST',
        url: '../myWebservice',
        dataType: "json",
        context: caller,
    });
    ajax.success(function(result,status,xhr){
        //*Do some processing to find condition*
        if (condition) {
        //myCall.settings.onSuccess(result); //shows 'undefined' &amp; 'undefined'
        //eval(myCall.settings.onSuccess)(result); //shows 'fooStr' &amp; 'fooStr'
        //this.eval(myCall.settings.onSuccess)(result); //shows 'fooStr' &amp; throws an object exception
        }
    });
};
</code></pre>

<p>The context of the success callback is correct, but it seems to lose it once the onSuccess functions are called. Am I going about this the correct way?</p>
