2014-02-13 49 views
0

我想在JS中傳遞一個對象(引入了一些jQuery)。 :)Javascript - 依賴注入和承諾

一旦promise.success函數運行,我想調用FlappyBarHelper.getUserPropertyCount()方法。我已經試過路過this.FlappyBarHelper到:

return $.ajax({ 
       type: "GET", 
       url: 'get-the-score', 
       flappy: this.FlappyBarHelper, 
      }); 

但仍然讓flappy未定義在promise.success

我完整的代碼:

function Rating(FlappyBarHelper) { 
    this.FlappyBarHelper = FlappyBarHelper; 
} 

Rating.prototype.attachRaty = function(property_id) 
{ 

    var promise = this.getPropertyScoreAjax(property_id); 

    promise.success(function (data) { 


     $('#'+property_id).raty({ 
      click: function (score, evt) { 

       $.ajax({ 
        type: "GET", 
        url: '/set-the-score', 
       }) 
        .done(function (msg) { 
         $('#extruderTop').openMbExtruder(true); 
          //**** FlappyBarHelper is undefined at this point ****/// 
         FlappyBarHelper.getUserPropertyCount('.flap'); 
        }); 


      } 
     }); 


    }); 

}; 

Rating.prototype.getPropertyScoreAjax = function(property_id) 
{ 

     return $.ajax({ 
      type: "GET", 
      url: 'get-the-score', 
     }); 
} 

回答

1

讀取從($阿賈克斯]的文檔( https://api.jquery.com/jQuery.ajax/

這個引用在所有的callbac ks是在設置中傳遞給$ .ajax的上下文選項中的對象;如果未指定上下文,則這是對Ajax設置本身的引用。

因此你應該通過您的變量以及你正在做的多個呼叫:

Rating.prototype.attachRaty = function(property_id){ 

    var promise = this.getPropertyScoreAjax(property_id); 
    // it's best to use done 
    promise.done(function (data) { 

    $('#'+property_id).raty({ 
     // use proxy to keep context when the click will be received 
     click: $.proxy(function(score, evt) { 
     $.ajax({ 
      type: "GET", 
      url: '/set-the-score', 
      // propagate your variable 
      FlappyBarHelper: this.FlappyBarHelper 
     }).done(function (msg) { 
      $('#extruderTop').openMbExtruder(true); 
      // here it should be defined 
      this.FlappyBarHelper.getUserPropertyCount('.flap'); 
     }); 
     }, this); 
    }); 
    }); 
}; 

Rating.prototype.getPropertyScoreAjax = function(property_id) { 
    return $.ajax({ 
    type: "GET", 
    url: 'get-the-score', 
    // propagate your variable 
    FlappyBarHelper: this.FlappyBarHelper 
    }); 
} 

你也可以考慮做一個閉包變量:

Rating.prototype.attachRaty = function(property_id){ 
    // here is the closure variable 
    var helper = this.FlappyBarHelper; 

    var promise = this.getPropertyScoreAjax(property_id); 
    // it's best to use done 
    promise.done(function (data) { 

    $('#'+property_id).raty({ 
     click: function(score, evt) { 
     $.ajax({ 
      type: "GET", 
      url: '/set-the-score' 
     }).done(function (msg) { 
      $('#extruderTop').openMbExtruder(true); 
      // you can still use the defined variable: power (and danger) of closures 
      helper.getUserPropertyCount('.flap'); 
     }); 
     }, this); 
    }); 
    }); 
}; 

Rating.prototype.getPropertyScoreAjax = function(property_id) { 
    return $.ajax({ 
    type: "GET", 
    url: 'get-the-score' 
    }); 
} 
+0

漂亮的教科書答案。感謝您的詳細解釋。我還閱讀了「成功vs完成」的區別。 :) – Kiksy

+0

不客氣! – Feugy