Link to external object in ajax callback function

A simplified example:

// Let create a new object
function MyObject() {
    //
}

// Add some method to this object
MyObject.prototype.myFunctionA = function() {
    //
}

// Another method
MyObject.prototype.myFunctionB = function(arg) {
    // AJAX GET request          
    $.get('script.php', { par : arg }, function(data) {

        // and here in the callback function    
        // I need to call MyObject.prototype.myFunctionA method!
        // but "this" references callback function so
        // I don't know how to access MyObject here

    });
}

I explained my problem in the comments. How can i do this?

+3
source share
3 answers

The simplest:

// Let create a new object
function MyObject() {
    //
}

// Add some method to this object
MyObject.prototype.myFunctionA = function() {
    //
}

// Another method
MyObject.prototype.myFunctionB = function(arg) {
    // AJAX GET request       
    var me = this;   
    $.get('script.php', { par : arg }, function(data) {
        // use me.something instead of this.something
    });
}

reuse (using area capture):

function createDelegate(obj, handler)
{
    return function() {
        handler.apply(obj, arguments);
    }
}

then

MyObject.prototype.myFunctionB = function(arg) {
    // AJAX GET request       
    var me = this;   
    $.get(
        'script.php', 
        { par : arg },
        createDelegate(this, function(data) {
        // use this.something
        })
    );
}

So, some code regarding the comments below, createDelegate can also be used in several ways, one of which is:

function createDelegate(obj, handler)
{
    handler = handler || this;
    return function() {
        handler.apply(obj, arguments);
    }
}

Function.prototype.createDelegate = createDelegate;

this allows you to do things like:

var someObj = {a:1, b:function() {return this.a;}};
var scopedDelegateForCallback = someObj.b.createDelegate(whateverobj)

you can do tricks to get a parent too, but it's too much for me to worry with atm.

or you can do something like this:

function createDelegate(handler, obj)
{
    obj = obj || this;
    return function() {
        handler.apply(obj, arguments);
    }
}

Object.prototype.createDelegate = createDelegate;

and use it:

someObj.createDelegate(someObj.b);

or maybe:

function createDelegateForMember(handlerName, obj)
{
    obj = obj || this;
    return function() {
        obj[handlerName].apply(obj, arguments);
    }
}

Object.prototype.createDelegate = createDelegateForMember;

then

someobj.createDelegate("b");
+7
source

"var self = this" MyObject.prototype.myFunctionB $.get "self" .

MyObject.prototype.myFunctionB = function(arg) {
    var self = this;

    $.get('script.php', { par : arg }, function(data) {
       alert(self);
    });
}
+6

JavaScript functions close in external areas, so you can do this:

// Another method
MyObject.prototype.myFunctionB = function(arg) {
    // Save `this` reference for use in callback.
    var that = this;

    // AJAX GET request
    $.get('script.php', { par : arg }, function(data) {

        // Now `that` holds the contents of the current `MyObject`.
        // So you can call other methods.
        that.myFunctionA();
        // ... etc ...

    });
}
+4
source

Source: https://habr.com/ru/post/1723293/


All Articles