0
在這裏淘汰賽很新穎。淘汰賽js applybinding
我從獲得所有經銷商的Web服務獲取JSON數據。我想將它綁定到GUI,然後能夠根據所選區域過濾數組,而不必從Web服務獲取新數據並應用。現在看來,我需要在jquery.getJSON函數中使用過濾和ko.applyBinding功能(在分隔符函數中,而不是在示例中的page.ready中)。我不想這樣,因爲這意味着我必須從Web服務獲取新數據,並在用戶每次想要過濾數組時都運行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;
});
}