[英]Update subsection of mapped knockout object
I know this question sounds familiar, but I have read so many of the similar questions and have not been able to find my specific situation. 我知道这个问题听起来很耳熟,但是我已经阅读了很多类似的问题,但是找不到我的具体情况。
I have a javascript object called gds (GlobalDataStructure). 我有一个名为gds(GlobalDataStructure)的JavaScript对象。 As its name suggests, this object contains all the information I need for all the web pages of my project and is stored in localStorage (hence why I am not just updating view model and need to map in the first place).
顾名思义,该对象包含项目所有网页所需的所有信息,并存储在localStorage中(因此,为什么我不只是更新视图模型,而且首先需要映射)。 It contains all sorts of feeds that are read via AJAX.
它包含各种通过AJAX读取的提要。
I use a constructor function to create gds. 我使用构造函数创建gds。 To massively simplify this structure and hopefully make this question simple, let's say gds was
为了大幅度简化此结构并希望简化此问题,假设gds是
function gdStructure(){
this.lastUpdate = new Date(2010,1,1);
this.family = new Object();
this.series = new Object();
this.product = new Object();
}
so I have 所以我有
gds= new gdStructure();
then once i have populated the js array with data from initial feeds, I do the following. 然后,一旦我用初始提要中的数据填充了js数组,就可以执行以下操作。
viewmodel = ko.mapping.fromJS(gds);
ko.mapping.fromJS(gds,viewmodel);
The view model is then bound to each page with 然后将视图模型绑定到每个页面
ko.applyBindings(viewmodel);
So this all works no problem. 因此,这一切都没有问题。 This issue arises when I, for example, get a new family feed and I want to update that object.
例如,当我获得一个新的家庭供稿并且想要更新该对象时,就会出现此问题。 I can do the following
我可以做到以下几点
gds.family=feed;
ko.mapping.fromJS(gds,viewmodel);
And all would work fine, but that is mapping a MASSIVE JS object every time. 一切都可以正常工作,但是每次都映射一个MASSIVE JS对象。 This is extremely slow so I need to find a way where I can update just the feed that has changed.
这非常慢,因此我需要找到一种方法来更新已更改的提要。 ideally something like..
理想的情况是..
ko.mapping.fromJS(gds.family, viewmodel.family);
But this appears not to work. 但这似乎不起作用。 Also because it is an OBJECT I can't just do.
也因为它是一个对象,所以我不能做。
viewmodel.family(gds.family)
Can anybody help me? 有谁能够帮助我? I am sure this must be so simple.
我相信这一定很简单。
Thanks in anticipation. 谢谢您的期待。
I think you have missunderstood how the lib works, this part looks very strange 我认为您误解了lib的工作原理,这部分看起来很奇怪
viewmodel = ko.mapping.fromJS(gds);
ko.mapping.fromJS(gds,viewmodel);
Your using the definition as data? 您使用定义作为数据吗?
Anyway, I use ko.mapping like this (A bit simplified but i think you get the point) 无论如何,我使用像这样的ko.mapping(有点简化,但我想你明白了)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.