我正在使用 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());