2012-11-27 3 views
0

꽤 녹아웃 여기에.녹아웃 js applybinding

모든 딜러가있는 웹 서비스에서 JSON 데이터를 가져옵니다. 이것을 GUI에 바인딩하고 나중에 웹 서비스에서 새로운 데이터를 가져오고 적용 할 필요없이 선택한 영역을 기반으로 배열을 필터링 할 수 있습니다. 이제 jquery.getJSON 함수 내에서 필터링 및 ko.applyBinding 기능 (예 : page.ready가 아닌 separete 함수)이 필요합니다. 나는 이것을 원하지 않는다. 왜냐하면 웹 서비스로부터 새로운 데이터를 얻어야하고 사용자가 배열을 필터링하려고 할 때마다 applybinding을 실행해야하기 때문이다.

이 코드는 매우 간단하지만, 난 당신이 포인트를 얻을 희망 :

function GridModel() { 
    var self = this; 
    self.Dealers = ko.observableArray(); 
} 

var Grid_Model;  

$(document).ready(function() { 
    Grid_Model = new GridModel(); 
    ko.applyBindings(Grid_Model); // If this line is here, no data is bound to the GUI at all 
    jQuery.getJSON("URL", function (data) { 
     Grid_Model.Dealers = ko.mapping.fromJS(data); 
     // If I put the filter functionality here, it works 
     // If I put ko.applyBindings(Grid_Model); here, it works. 
    }); 
}); 

function filterDealers(string region) { 
    Grid_Model.Dealers = ko.utils.arrayFilter(Grid_Model.Dealers(), function(dealer) {     
     return dealer.RegionName() == region; 
    }); 
} 

답변

0

당신은 바인딩마다 적용 호출 할 필요는 없습니다. 코드를 아래와 같이 변경하십시오.

jQuery.getJSON("URL", function (data) { 
    var dealerArray = ko.mapping.fromJS(data); 
    // Filter the dealer array here 
    Grid_Model.Dealers(filteredArray); 
});