我想使用Knockout.js构建REST服务的客户端。我有很多存储库需要通过不同的URL进行访问 - 因此,我想出了使用Revealing-Prototype-Pattern的解决方案。
问题:我无法找到如何将ItemsProperty映射到从服务接收到的"data"。
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(data,self.Items);但是我找不到正确的做法。
问题:我做错了什么? 我已经找到一个例子-他们做的应该是一样的:http://jsfiddle.net/jearles/CGh9b/