简体   繁体   English

KnockoutJS:阻止特定属性设置脏标志

[英]KnockoutJS: Stop a particular property from setting dirty flag

With some help from StackOverflow community I was able to get my dirty flag implementation to work, based on this example: http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html 在这个示例的帮助下,在StackOverflow社区的一些帮助下,我能够使我的脏标志实现起作用: http : //www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html

It does exactly what I want, except for a single use case that I don't know how to solve. 除了我不知道如何解决的单个用例外,它完全可以满足我的要求。

Basically I have a select menu that gets automatically populated from the database. 基本上,我有一个选择菜单,该菜单从数据库自动填充。 This select menu also has an option to make an Ajax call to my back end and have the list of options refreshed, database updated and return the result . 该选择菜单还具有一个选项,可以对我的后端进行Ajax调用,并刷新选项列表,更新数据库并返回result This is where things get hairy for me. 这就是让我毛茸茸的地方。

First method works fine, however, it has to re-index and re-apply my entire viewModel and takes about 2-3 seconds, running on a local machine with 16gigs of ram and SSD. 第一种方法可以正常工作,但是,它必须重新索引并重新应用我的整个viewModel并且需要大约2-3秒的时间,才能在具有16gig ram和SSD的本地计算机上运行。

jsondata.component.available_tags = result.available_tags;
ko.mapping.fromJS(jsondata, viewModel);

Second method also works, and pretty much instantaneous, however, it sets of isDirty() flag, which I would like to avoid, because this data is already coming from the database and I wont need to save it. 第二种方法也可以工作,并且几乎是瞬时的,但是它设置了isDirty()标志,我想避免使用它,因为该数据已经来自数据库,并且我不需要保存它。 I can not use isDirty.reset() method either, because if isDirty was set by something else before I clicked an menu option to update available_tags , it will reset that too. 我也不能使用isDirty.reset()方法,因为如果在单击菜单选项以更新available_tags之前, isDirty是由其他设置的,它也会重置它。 Which I would also like to avoid. 我也想避免。

viewModel().component.available_tags(result.available_tags);

My question is: With the first method, can I force UI refresh with ko.mapping.fromJS() on a particular element and not entire dataset? 我的问题是:使用第一种方法,是否可以在特定元素而非整个数据集上强制使用ko.mapping.fromJS()刷新UI? Or, with a second method, can I avoid setting isDirty flag set when available_tags are updated? 或者,使用第二种方法,可以在更新available_tags时避免设置isDirty标志吗? The twist is that I still need to keep available_tags as an observable, so the select menu is automatically generate/updated. 所不同的是,我仍然需要将available_tags保持为可观察的状态,因此选择菜单会自动生成/更新。

UPDATE: I was able to update mapping for that one single element with 更新:我能够使用以下命令更新该单个元素的映射

ko.mapping.fromJS(result.available_tags, {}, viewModel().component.available_tags);

but that immediately set off isDirty flag... Argh 但是那立即引起了isDirty标志... isDirty

In addition to Tomalak's suggestions, which I totally agree with, maybe the toJSON method can help you out in similar cases where you don't want to split the model. 除了我完全同意的Tomalak的建议以外,也许toJSON方法可以在不希望拆分模型的类似情况下为您提供帮助。 If your dirty flag implementation uses ko.toJSON as a hash function, as Ryan Niemeyer's does, you can give your model (on which the dirty flag is active) a toJSON method, where you do something like this: 如果像Ryan Niemeyer一样,如果脏标志实现使用ko.toJSON作为哈希函数,则可以为模型(脏标志在其上处于活动状态)提供toJSON方法,您可以在其中执行以下操作:

function MyObjectConstructor() {
    this.someProperty = ko.observable();
    this.somePropertyNotUsedInDirtyFlag = ko.observable();
}
MyObjectConstructor.prototype.toJSON = function () {
    var result = ko.toJS(this);
    delete result.somePropertyNotUsedInDirtyFlag;
    return result;
};

Please be aware that this is also used to serialize the object in some other occassions, such as ajax calls. 请注意,这在某些其他情况下(例如ajax调用)也用于序列化对象。 It's generally a handy function for removing computeds and such from your objects before using them in a different context. 通常这是一个方便的函数,用于在不同上下文中使用对象之前从对象中删除计算等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM