[Jquery-Es] Objetos + jquery + ajax
Choan Gálvez
choan.galvez en gmail.com
Vie Ago 3 13:35:24 PDT 2007
Hola.
On 03/08/2007, at 17:36, rey wrote:
> Hola listeros,
>
> Llevo algún tiempo liando con este Script y la verdad no se cómo
> solucionarlo:
>
> ////////////////
>
> obj = function(){
> this.id = "miId";
> }
>
> obj.prototype.send = function(){
> alert(this.id); // retorna "miId"
> $.ajax({
> url:"../index.html",
> success:this.callBack
> });
> }
> obj.prototype.callBack = function(){
> alert(this.id); // retorna "undefined"
> //Cómo puedo solucionar esto para que me
> retorne el
> valor de mi variable osea "miId"
> }
>
> var miObj = new obj;
> miObj.send();
>
> ////////////////
>
> Quiero que cuando termine se ejecute el evento del request AJAX me
> tome el
> valor de la variable "id" del objeto que envió la petición.
>
> Si alguno de ustedes puede darme una luz les quedaría agradecido.
Pues por aquí tengo un mechero ;)
Tal cual lo tienes, dentro del callback `this` apunta al objeto
XMLHttpRequest. Para hacerlo como pretendes, tendrás que usar una
closure:
obj.prototype.send = function(){
var that = this;
$.ajax({
url:"../index.html",
success: function(r) { that.callBack(r); }
});
};
Salud.
--
Choan Gálvez
<choan.galvez en gmail.com>
<http://choangalvez.nom.es/>
Más información sobre la lista de distribución Jquery-Es