javascript - Prototype and Ajax.Request scope - Stack Overflow

I'm having trouble getting the correct scope within prototype's Ajax.Request class. What I�

I'm having trouble getting the correct scope within prototype's Ajax.Request class. What I'm trying to do is write a simple API which 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});
    // and so on...
  },

  _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'm in the onSuccess() method I expected +this+ to refer to the parent object, but it is giving me DOMWindow. I can't seem to get that response data into the API class at all. I figure it is something stupid (binding?), but I just can't seem to think this one out today.

Thanks

I'm having trouble getting the correct scope within prototype's Ajax.Request class. What I'm trying to do is write a simple API which 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});
    // and so on...
  },

  _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'm in the onSuccess() method I expected +this+ to refer to the parent object, but it is giving me DOMWindow. I can't seem to get that response data into the API class at all. I figure it is something stupid (binding?), but I just can't seem to think this one out today.

Thanks

Share Improve this question edited Dec 28, 2011 at 21:50 Rob W 349k87 gold badges807 silver badges682 bronze badges asked May 21, 2009 at 15:59 RobHRobH 5685 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Okay. I missed the bigger problem. I was requesting asynchronously so it was setting the result, just not immediately. To be fair, it was also a binding issue. Here is the proper request:

_request:function(resource, params)
{
  var uri = this.request_uri.evaluate({"api_token":this.api_token,"resource":resource});
  new Ajax.Request(uri,
  {
    asynchronous: false,
    contentType:'application/json',
    method:'get',
    parameters:params,
    onSuccess:function(r)
    {
      this.last_result = r.responseJSON;
      this.status = r.status;
    }.bind(this)
  });    

  alert(this.status);

  return this.last_result;
}

Your solution should NOT work?

You need to use local variables above the nested function and then convert them into the 'this' scope:

 _request:function(resource, params)
    {
      var uri = this.request_uri.evaluate({"api_token":this.api_token,"resource":resource});
      var last_result = "";
      var status = "";
      new Ajax.Request(uri,
      {
        asynchronous: false,
        contentType:'application/json',
        method:'get',
        parameters:params,
        onSuccess:function(r)
        {
          last_result = r.responseJSON;
          status = r.status;
        }
      });  
      this.last_result = last_result;
      this.status = status;  
      alert(this.status);
      return this.last_result;
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744396627a4572173.html

相关推荐

  • javascript - Prototype and Ajax.Request scope - Stack Overflow

    I'm having trouble getting the correct scope within prototype's Ajax.Request class. What I�

    8天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信