在Knockout JS映射插件中更新子属性 [英] Updating Sub Properties in Knockout JS mapping plugin

查看:56
本文介绍了在Knockout JS映射插件中更新子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了淘汰赛js和具有分层视图模型的映射插件的问题

I'm having an issue with knockout js and mapping plugin with a hierarchical view model

我的视图模型的结构如下:

My viewmodel is structured somewhat like this:

VM = {
    members:[
        {
            name:"name 1",
            volunteering:[{...},{...},{...}]
        },
        {
            name:"name 1",
            volunteering:[{...},{...},{...}]
        }
    ]
}

每个成员都在一个选项卡中,每个选项卡都有一个志愿者活动网格.单击网格中的项目会弹出一个对话框,以编辑志愿活动. 此时,我克隆对象以促进取消编辑"功能

Each member is in a tab, and each tab has a grid of volunteering activities. Clicking on an item in the grid pops up a dialog box to edit the volunteering activity. At this point I clone the object to facilitate 'cancel edit' functionality

var Volunteer = {};
var koContext=ko.contextFor(this);
Volunteer = ko.mapping.toJS(koContext.$data);  //plain js volunteer
Volunteer.index=koContext.$parent.EventVolunteers().indexOf(koContext.$data);  //index of volunteer in member volunteer array
ko.applyBindings(ko.mapping.fromJS(Volunteer),$("#dialog-EditVolunteer")[0]); //bind new volunteer obj to dialog

到目前为止,看起来还可以,单击对话框上的保存"会导致问题.

Up to this point seems ok, clicking save on the dialog causes the issue.

var volunteer = ko.mapping.toJS(ko.contextFor(this).$data);
ko.mapping.fromJS(volunteer,{},ko.contextFor(currentTab).$data.EventVolunteers()[volunteer.index]);

这时,属性会在视图模型中更新,但不会在主屏幕的网格中更新.

At this point the properties get updated in the viewmodel, but not in the grid on the main screen.

似乎ko.mapping.fromJS取代了可观察对象,而不是对其进行了更新.

推荐答案

在这种情况下,我最终的解决方案是使用克隆"视图模型中的编辑值在原始VM上设置属性.

My eventual solution in this case was to set the properties on the original VM, with the edited values from the 'cloned' viewmodel.

但是对于新项目,我现在使用淘汰插件

For new projects however, I now use a knockout plugin

这篇关于在Knockout JS映射插件中更新子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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