2014-02-13 3 views
0

JS에 객체를 전달하려고합니다 (일부 jQuery가 포함됨). :)Javascript - 종속성 삽입 및 약속

promise.success 함수가 실행되면 FlappyBarHelper.getUserPropertyCount() 메서드를 호출하고 싶습니다. I가 this.FlappyBarHelper 전달 시도했다 :

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

하지만 여전히 promise.success

내 전체 코드에서 정의되지 않은 flappy을 만드는 것은 ($ 아약스]의 문서에서

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

꽤 교과서 대답. 자세한 설명 주셔서 감사합니다. 나는 또한 '성공 대 완료'라는 차이점을 읽었습니다. :) – Kiksy

+0

당신을 환영합니다! – Feugy