Magento / JavaScript / Prototype:需要有关嵌套AJAX调用中名称空间的信息

时间:2012-02-20 22:44:57

标签: javascript ajax magento prototypejs

我正在尝试修改Magento中的一些代码来创建一对AJAX调用,这样第二个调用只有在第一个调用成功时才会激活。具体来说,我正在尝试在结账时将一些额外的地址验证代码插入到结算页面中,这样只有当地址验证为有效的邮政地址时才会发生Magento验证+表格发布(通过AJAX)。

我的问题来自于尝试引用内部调用中的某些函数,我遇到了一些名称空间问题。示例代码:

var Billing = Class.create();
Billing.prototype = {
initialize: function(form, addressUrl, saveUrl){
    this.form = form;
    if ($(this.form)) {
        $(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
    }
    this.addressUrl = addressUrl;
    this.saveUrl = saveUrl;
    this.onAddressLoad = this.fillForm.bindAsEventListener(this);
    this.onSave = this.nextStep.bindAsEventListener(this);
    this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},

save:  function() {
    //(initialize things for first call, e.g., URL to use):
    var addressRequest = new Ajax.Request(url, 
        {
          method: 'get',
          onSuccess: function(response)  {
              // (do some things with the results...)

              // (initialize things for second call, which was 
              // the stock Magento AJAX call to validate form fields 
              // and post the results to another Web page...)

              var request = new Ajax.Request(
                  this.saveUrl, // no longer in scope
                  {
                        asynchronous: true,
                        method: 'post',
                        onComplete: this.onComplete, // also no longer in scope
                        onSuccess: this.onSave, // also no longer in scope
                        onFailure: checkout.ajaxFailure.bind(checkout), // "checkout" is a distinct object and remains in scope
                        parameters: Form.serialize(this.form) // this.form no longer in scope
                  }
              );
          }
      }
  },

  fillForm: function(transport) { ... }, 

  nextStep:  function(transport) { ... },

  resetLoadWaiting: function(transport) { ... },

原始的“this.xxx”引用(例如this.form和this.onComplete)在嵌套调用中不再在范围内。我想知道如何在任何嵌套深度正确引用它们(我可能还想在这里做几个其他的AJAX调用)。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

尝试

onSuccess: function(response)  {
...
}.bind(this);