尝试使用映射将数组从JSON映射到敲除 [英] Trying to map array from JSON to knockout using mappings

查看:123
本文介绍了尝试使用映射将数组从JSON映射到敲除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的JSON数组

Hi I have a JSON array like so

[{
    "id": "537901a53513fa3374bec718",
    "images": [],
    "itemImage": "img/3.jpg",
    "createdDate": "5/18/2014 6:53:25 PM",
    "location": [
        -2.057802,
        52.609711
    ]
},
{
    "id": "537901a53513fa3374bec710",
    "images": [
        "img/17.jpg"
    ],
    "itemImage": "img/1.jpg",
    "createdDate": "5/18/2014 6:53:25 PM",
    "location": [
        -2.062251,
        52.612397
    ]
}]

我正在尝试使用ko.mapping.fromJSON/JS,但我一直在叹息:(

I'm trying to use the ko.mapping.fromJSON / JS, but i keep messing up sigh :(

  var viewModel = {};

在我的Ajax成功功能中,

in my Ajax success function,

  viewModel.model = ko.mapping.fromJSON(data);
  ko.applyBindings(viewModel);

HTML

   <div data-bind=" foreach: model">
       <div data-bind="text: body"></div>
   </div>            

我已经尝试过约翰·帕帕(John Papa)回答的这篇文章,但我认为我的观点与众不同. 淘汰赛JS映射插件混乱 我可以手动完成,但是对手动构建视图模型很无聊:).

I've tried following this post answered by John Papa, but I think my array is different. Knockout JS mapping plugin confusion I can do it manually, but am bored of manually building view models :).

也尝试过此操作,并进一步感到困惑可以吗? t将JSON对象映射到ViewModel(删除模板)

Also tried this and got confused further Can't map JSON object to ViewModel (knockout js)

有什么好心的人能指出我正确的方向吗?

Would any kind souls be able to point me in the right direction ?

推荐答案

您需要做的只是以下事情:

All you need to do is the following:

var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);

您可以如下循环它们:

<table class="table table-striped">
        <tbody data-bind="foreach: $data">
            <tr>

                <td data-bind="text: id"></td>
            </tr>
        </tbody>
    </table>

在此处查看示例:

http://jsfiddle.net/wr5W7/5/

这篇关于尝试使用映射将数组从JSON映射到敲除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆