I'm having trouble getting the correct scope in the prototype of the Ajax.Request class. What I'm trying to do is write a simple API that wraps ajax requests:
API = Class.create({
initialize:function(api_token)
{
this.api_token = api_token;
this.request_uri = new Template('/api/#{api_token}/#{resource}.json');
this.status = 0;
this.last_result = null;
},
some_api_call:function()
{
var result = this._request('resource', {'id':1});
},
_request:function(resource, params)
{
var uri = this.request_uri.evaluate({"api_token":this.api_token,"resource":resource});
new Ajax.Request(uri,
{
contentType:'application/json',
method:'get',
parameters:params,
onSuccess:function(r)
{
alert(this);
this.last_result = r.responseJSON;
this.status = r.status;
}
});
return this.last_result;
}
});
When I am in the onSuccess () method, I expected + this + to refer to the parent object, but it gives me DOMWindow. It seems I can not get the response data in the API class at all. I suppose this is something stupid (mandatory?), But I just can't think about it right now.
thanks
Robh source
share