0
我正在使用knockout.js構建一個排序列表。我有一個工作與一個簡單的可觀察數組的版本,但我怎樣才能使它與我的JSON數據和使用映射插件?使用knockout.js和json數據分類列表
http://jsfiddle.net/infatti/x66Ts/
// Here is my json data
var viewModel;
$.getJSON('http://echo.jsontest.com/name/Stuart', function (data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
// Here is my working version of the simple observable array. How can it work using json data and the mapping plugin?
var ListSortModel = function() {
// my items
this.allItems = ko.observableArray([
{ name: 'Denise' },
{ name: 'Charles' },
{ name: 'Bert' }
]);
// sorter
this.sortItems = function() {
this.allItems(this.allItems().sort(function(a, b) { return a.name > b.name;}));
};
};
ko.applyBindings(new ListSortModel());
我想仍然使用映射插件映射來自json的數據。 – simple
@簡單更新,更多解釋 –