簡體   English   中英

Magento / JavaScript / Prototype:需要有關嵌套AJAX調用中的名稱空間的信息

[英]Magento/JavaScript/Prototype: Need information about namespaces in nested AJAX calls

我正在嘗試修改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調用)。 任何建議將不勝感激!

嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM