如何使用基因剔除映射? [英] How to use knockout mapping?

查看:97
本文介绍了如何使用基因剔除映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将绑定应用于以下视图模型

I have applied bindings to the following view model

var groupDeleteViewModel = {
        group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() },
        load: function (item) {
            debugger

        },
        remove: function (item) {
            groupDeleteViewModel.group.Id(item.Id());
            groupDeleteViewModel.group.Name(item.Name());
            groupDeleteViewModel.group.Members(item.Members());
            $("#groupDelete").show();
        },
        cancel: function () {
            $("#groupDelete").hide();
        }
    }

remove函数使用在 item 中传递到remove函数的数据加载视图.

the remove function loads the view with the data that has been passed in item to the remove function.

<div id="groupDelete" class="pop-window filter-view">
    <h2>
        Delete Group
    </h2>
    <table>
        <tr>
            <th>
                Name
            </th>
            <td>
                <span data-bind="text:group.Name" />
            </td>
        </tr>
        <!--ko foreach: group.Members-->
        <tr>
            <th>
            </th>
            <td>
                <div data-bind="text:Name" class="grey-border">
                </div>
            </td>
            <td>
            </td>
        </tr>
        <!--/ko-->
    </table>
    <span class="centeralign">
        <input type="button" value="Delete" data-bind="click: remove" class="delete" />
        <input type="button" value="Cancel" data-bind="click: cancel" />
    </span>
</div>

我不想一直将 item 的每个元素映射到 groupDeleteViewmodel.group 的每个元素.我在代码的许多其他地方也做过,这使代码真的很凌乱.我想使用ko.mapping插件来做同样的事情.

I don't want to keep mapping each element of item to each element of groupDeleteViewmodel.group. I have done it at a lot of other places also in the code which has made the code really messy. I want to use the ko.mapping plugin to do the same thing.

到目前为止,我尝试过的是-

Till now what I have tried is -

remove:function (item){
            var data = ko.mapping.toJS(item);
            ko.mapping.fromJS(data, groupDeleteViewModel.group);
            $("#groupDelete").show();
}

但这是行不通的.我真的不知道为什么.它应该已经理想地工作了. 有人可以告诉在这种情况下使用ko.mapping的正确方法是什么吗?

But this just does not work. i really don't know why. It should have worked ideally. Can someone tell what is the right way of using ko.mapping in such a case?

谢谢.

推荐答案

映射插件正在查找groupDeleteViewModel.group中已经存在的映射数据,但是找不到,因为您没有使用来初始化groupDeleteViewModel.group映射插件. 因此,与其像您那样初始化组:

The mapping plugin is looking for mapping data already present in groupDeleteViewModel.group, but it doesn't find it, because you didn't initialize groupDeleteViewModel.group using the mapping plugin. So instead of initializing group like you did:

group: { Id: ko.observable(), Name: ko.observable(), Members: ko.observableArray() }

使用映射插件将其初始化:

initialize it using the mapping plugin:

group: ko.mapping.fromJS({ Id: undefined, Name: undefined, Members: [] })

这是我在弄弄您的代码:小提琴

And this is me fiddling around with your code: fiddle

这篇关于如何使用基因剔除映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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