Knockout.jsを使用してRESTサービスのクライアントを構築したいとします。 私はさまざまなURLからアクセスしたいリポジトリをたくさん持っています。そのため、Revealing-Prototype-Patternを使ってこのソリューションを考え出しました。 私の問題:自分のサービスから受け取った「データ」でItemsPropertyをマップする方法がわかりません。Knockout.js JSONを観測可能な配列にマッピングする
var Repository = function (url) {
this.Url = url;
this.Items = ko.observableArray([]);
this.PendingItems = ko.observableArray([]);
};
Repository.prototype = function() {
var
getAllItems = function() {
var self = this;
$.getJSON(self.Url, function (data) {
// data=[{"Id":1,"Name":"Thomas","LastName":"Deutsch"},{"Id":2,"Name":"Julia","LastName":"Baumeistör"}]
ko.mapping.fromJS(data, self.Items);
});
},
...
// i call it like this:
customerRepository = new Repository('http://localhost:9200/Customer');
customerRepository.getAllItems();
私はこの問題は、この中にあると思う:ko.mapping.fromJS(データ、self.Items)。しかし、私はそれを行う正しい方法を見つけることができません。
質問:何が間違っていますか? 012Z
この例の真ん中の空のオブジェクトリテラルは、みんなのために、マッピングオプションオブジェクトの空のプレースホルダーです。 – pimbrouwers