Json 在Knockout中映射数组

Json 在Knockout中映射数组,json,knockout.js,knockout-mapping-plugin,Json,Knockout.js,Knockout Mapping Plugin,以下是我对MVC代码的嘲弄: $(function(){ var ViewModel = function(){ this.items = ko.observableArray(null); this.isLoading = ko.observable(true); }; var data = [{"CreatedAt":"2013-12-29T22:00:20","Intro":"","Body":"","Title":"Test Ite

以下是我对MVC代码的嘲弄:

$(function(){

    var ViewModel = function(){
      this.items = ko.observableArray(null);
      this.isLoading = ko.observable(true);  
    };

    var data = [{"CreatedAt":"2013-12-29T22:00:20","Intro":"","Body":"","Title":"Test Item","Url":"/news-items/test-item/"},{"CreatedAt":"2013-12-29T21:13:34","Intro":"","Body":"","Title":"Test 1","Url":"/news-items/test-1/"},{"CreatedAt":"2013-12-29T16:03:56","Intro":"","Body":"<p>In the spirit of Christmas we are holding a Christmas photo competition for all members to enter. Prizes will be given to the best Christmas themed photo and the funniest photo.&nbsp; To enter, simply email your photo to: competition@bernese.org.nz. Your entry will be uploaded onto the club's Facebook page where all members can then vote by 'liking' their favourite photo.</p>\n<p>Entries close on the 20th of December and voting will be open until the 5th of January. The winning photo's will be displayed on the website.</p>","Title":"Christmas 2013 Photo Competition","Url":"/news-items/christmas-2013-photo-competition/"}];
  var vm = new ViewModel();
  ko.applyBindings(vm);
  vm.items(test);
  vm.isLoading(false);
})
$(函数(){
var ViewModel=函数(){
this.items=ko.observearray(空);
this.isLoading=ko.可观察(真);
};
var data=[{“CreatedAt”:“2013-12-29T22:00:20”,“简介”:“正文”:“标题”:“测试项目”,“Url”:“/新闻项目/测试项目/”},{“CreatedAt”:“2013-12-29T21:13:34”,“简介”:“正文”:“标题”:“测试1”,“Url”:“新闻项目/Test-1/”,{“CreatedAt”:“2013-12-29T16:03:56”,“简介”:“正文”:"本着圣诞节的精神,我们将举办一场圣诞摄影比赛,所有会员均可参加。最佳圣诞主题照片和最有趣的照片将获得奖品。要参加比赛,只需将您的照片发送至:competition@bernese.org.nz.您的参赛作品将上传到俱乐部的Facebook页面,所有会员都可以通过“喜欢”进行投票他们最喜欢的照片。

\n参赛作品将于12月20日截止,投票将开放至1月5日。获奖照片将显示在网站上。

“,“标题”:“2013年圣诞节摄影比赛”,“Url”:“/新闻项目/Christmas-2013-photo-Competition/”}]; var vm=new ViewModel(); ko.应用绑定(vm); 虚拟项目(测试); vm.isLoading(false); })
我从MVC代码中模拟了它,但是
数据
对象基本上是从我的控制器返回的。在这种情况下,敲除映射不起作用,我怀疑这是我返回数据的方式。这是一种有效的方式,还是我需要用DTO进行排序,例如:
{items:[{item1:''.},{item2:''.}}}

谢谢

编辑: 我的错误是,我已经将
定义为
observableArray
。我使用这种方式,以便在页面加载加载程序后立即显示gif。我以前使用过这种方式,这次唯一的区别是返回的json格式


补充:这是我不知道这是否是唯一的问题,但是

this.items = ko.observable(null);  
应该是

this.items = ko.observableArray();
但我可能更喜欢这样做

$(function(){
    var ViewModel = function(items){
        this.items = ko.observableArray(items);  
    };

    var data = [...];
    var vm = new ViewModel(data);
})

更新:

如果
数据
参数已经是数组,则
ko.mapping.fromJS(数据)
返回一个
ko.observearray

因此,在分配之前,您需要打开返回的
ko.observableArray

var vm = new ViewModel();
ko.applyBindings(vm);
var test = ko.mapping.fromJS(data);
vm.items(test()); // you need to write `test()` to get the underlaying array.
或者您可以直接填写您已经声明的
ko.observableArray
,并写下:

var vm = new ViewModel();
ko.applyBindings(vm);
ko.mapping.fromJS(data, {} /* mapping options */, vm.items);

这是您的最新版本。

谢谢,我也试过了,但它也不起作用。它似乎是
ko.mapping.fromJS(数据)的输出
是正确的,所以这里我肯定遗漏了一些基本的东西。这是我的最新示例:我用一个工作的JSFIDLE更新了我的答案。主要问题是映射observableArray两次-您不需要,因为fromJS会为您返回一个可观测数组。