それはそれがやるべきことです(少なくとも理論/ドキュメントでは)が、どうやらそれはやっていることではありません。私は同じ問題を抱えていますが、他にもあると思います: https://groups.google.com/forum/?fromgroups=#!topic/knockoutjs/uKY84iZaxcs
オブジェクトは次の条件を満たす必要があります。
{ "someName" : [ { x: 1, y: "test" } ] }
オブジェクト スキーマに固執するには、ko.utils.arrayMap を使用してオブジェクトを KO ViewModel にマップします: http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html
function Item(name, category, price) {
this.name = ko.observable(name);
this.category = ko.observable(category);
this.price = ko.observable(price);
this.priceWithTax = ko.dependentObservable(function() {
return (this.price() * 1.05).toFixed(2);
}, this);
}
//do some basic mapping (without mapping plugin)
var mappedData = ko.utils.arrayMap(dataFromServer, function(item) {
return new Item(item.name, item.category, item.price);
});
編集
これについてさらに調査を行ったところ、実際には JS 配列オブジェクトを KO マッピングでマップできますが、マップ後のオブジェクトは KO Observable Array にはなりません。これは単なる通常の JS 配列オブジェクトであり、さらに言えば、KO でデータバインドできます。
var bd = [ { x: 1, y: "bd test" }, { x: 2, y: "bd test 1dsf" } ];
var bdViewModel = ko.mapping.fromJS(bd);
// 'bdViewModel' is NOT KO Observable Array, so you can't use KO Binding. However, all the properties of 'bdViewModel' (x and y) are KO Observable.
//ko.applyBindings(bdViewModel, $("#bd").get(0));
console.log(bdViewModel());
// 'bdViewModel' must be called as function (with open and close parentheses) to see the data.
$.each(bdViewModel(), function (i, d) {
$("#bdList").append("<li>" + d.y() + "</li>");
});
マッピング JS 配列と JSON を比較するための JSBin は次のとおりです: http://jsbin.com/uzuged/5/