2011-08-09 8 views
6

Tengo un problema con OO Javascript y una devolución de llamada jQuery. Si observa la muestra a continuación, debe explicar todo.JQuery: hace referencia al ámbito externo dentro de la devolución de llamada

¿Cómo llamo a functionToCall() en esta función?

function outerClass() { 
    this.functionToCall = function() { 
     //do something 
    } 

    this.someOtherFunction = function() { 

    this.aCoupleOfVariables1 = 2; 
    this.aCoupleOfVariables2 = "stuff"; 

    $.ajax({ 
     success: function() { 
     //How do I call functionToCall() right here 
     //TRIED: 
      functionToCall(); 
      this.functionToCall(); 
      that.functionToCall(); 
     } 
    }); 
    } 
} 

Respuesta

14

Puede pasar this como la opción context a $.ajax():

$.ajax({ 
    context: this, 
    success: function() { 
     // Here, 'this' refers to the same object as in the caller. 
     this.functionToCall(); 
    } 
}); 
+3

Ooo, me gusta mucho más. – Andrew

+2

Solo una nota al margen para el OP: siempre que necesite hacer uso de 'this' y no puede usar' context' ni ninguna otra propiedad para el caso (muchos casos), http://api.jquery.com/jQuery .proxy/ – yoda

+0

Bueno, esto evita tener que usar $ .proxy también. –

6

tener una referencia local para que,

function outerClass() { 

    var self = this; 

    this.functionToCall = function() { 
    //do something 
    } 

    this.someOtherFunction = function() { 

    this.aCoupleOfVariables1 = 2; 
    this.aCoupleOfVariables2 = "stuff"; 

    $.ajax({ 
     success: function() { 
      self.functionToCall(); 
     } 
    }); 
    } 
} 
5

es necesario definir that en el perímetro exterior.

function outerClass() { 
    var that = this; 

    // ... 

    $.ajax({ 
     success: function() { 
      that.functionToCall(); 
     } 
    }); 
} 
4

necesita almacenar la referencia para el this value del alcance de los padres:

var parentScope = this; 

y luego acceder a functionToCall a través de ese objeto

parentScope.functionToCall(); 

Ejemplo:

function outerClass() { 
    var parentScope = this; 

    this.functionToCall = function() { 
     //do something 
    } 

    // ... 

    $.ajax({ 
     success: function() { 
      parentScope.functionToCall(); 
     } 
    }); 
} 

Otra forma de lograr eso sería utilizar ES5 de .bind() para establecer el valor de this dentro de su función interna (-context)

$.ajax({ 
    success: function() { 
     // Here, 'this' refers to the same object as in the caller. 
     this.functionToCall(); 
    }.bind(this) 
}); 
2

Mantenga el alcance de la función en una variable local y úsela o también puede usar jquery proxy en la que puede especificar el contexto.

function outerClass() { 
    this.functionToCall = function() { 
     //do something 
    } 

    this.someOtherFunction = function() { 

    this.aCoupleOfVariables1 = 2; 
    this.aCoupleOfVariables2 = "stuff"; 


    $.ajax({ 
     success: $.proxy(function() { 
      this.functionToCall(); 
     }, this) 
    }); 
    } 
} 
Cuestiones relacionadas