2012-08-17 3 views
0

누구나 ViewModel에서 체인 종속 속성의 작동 예제를 가지고 있습니까? 코드 예제 내가 무슨 말을하는지 설명 할 것입니다 : KnockoutJS 체인 종속 속성

<script type="text/javascript"> 
    var ViewModel = function() { 
     this.SelectedPubCode = ko.observable('@Model.PubCode'); 
     this.SelectedVersionName = ko.observable('@Model.VersionName'); 
     this.PubCodes = ko.observable(@Model.PubCodes.ToJavascriptArray()); 
     this.VersionNames = ko.observable(@Model.VersionNames.ToJavascriptArray()); 

     this.LoadVersionNames = function(pubCode) { 
      var self = this; 
      $.ajax({ 
       type: "POST", 
       url: '@Url.Action("LoadVersionNames")', 
       data: "pubCode=" + pubCode, 
       success: function(data) { 
        self.VersionNames(data); 
        // Trigger chain call (will call SelectedVersionName subscribtion). 
        self.SelectedVersionName(''); 
       } 
      }); 
     }; 

     this.SelectedPubCode.subscribe(function(newPubCode) { 
      // Accessing public variable to call model's method 
      model.LoadVersionNames(newPubCode); 
     }); 

     this.SelectedVersionName.subscribe(function(newValue) { 
      // Another model's method can be called here. 
      alert("Version Name has been changed to '" + newValue + "'"); 
     }); 
    }; 
    // Creating public variable to access it from inside of subscribtion action 
    var model = new ViewModel(); 
    ko.applyBindings(model); 
</script> 

this.SelectedPubCode.subscribe(function(newPubCode) 전화를보고하십시오. 이 작업을 수행하려면 전역 변수를 사용해야합니다. 원하는 동작을 수행 할 수있는 다른 방법이 있습니까?

답변

2

녹아웃 팀은이 포인터를 저장하기 위해 뷰 모델에서 자체 변수를 사용하는 것이 좋습니다. 이 경우 전역 변수를 사용하는 대신 self를 사용하여 LoadVersionNames 함수를 호출 할 수 있습니다.

귀하의 코드는 다음과 같이 보일 것입니다

:

<script type="text/javascript"> 
    var ViewModel = function() { 
     var self = this; 
     self.SelectedPubCode = ko.observable('@Model.PubCode'); 
     self.SelectedVersionName = ko.observable('@Model.VersionName'); 
     self.PubCodes = ko.observable(@Model.PubCodes.ToJavascriptArray()); 
     self.VersionNames = ko.observable(@Model.VersionNames.ToJavascriptArray()); 

     self.LoadVersionNames = function(pubCode) { 
      $.ajax({ 
       type: "POST", 
       url: '@Url.Action("LoadVersionNames")', 
       data: "pubCode=" + pubCode, 
       success: function(data) { 
        self.VersionNames(data); 
        // Trigger chain call (will call SelectedVersionName subscribtion). 
        self.SelectedVersionName(''); 
       } 
      }); 
     }; 

     self.SelectedPubCode.subscribe(function(newPubCode) { 
      // Accessing public variable to call model's method 
      self.LoadVersionNames(newPubCode); 
     }); 

     self.SelectedVersionName.subscribe(function(newValue) { 
      // Another model's method can be called here. 
      alert("Version Name has been changed to '" + newValue + "'"); 
     }); 
    }; 
    // Creating public variable to access it from inside of subscribtion action 
    var model = new ViewModel(); 
    ko.applyBindings(model); 
</script> 
+0

대단히 감사합니다! 구독 통화에서 '자기'가 액세스 할 수 없다는 것이 확실했습니다. – Anubis