淘汰赛JS-脏旗问题 [英] Knockout js - Dirty Flag issue

查看:79
本文介绍了淘汰赛JS-脏旗问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看页面上使用了淘汰赛Js.我有一个要求,如果有任何可编辑的字段发生更改,则必须启用保存"按钮,否则不启用.这很好. 我的问题是,项目的每一行也都有复选框.这些是我的viewModel中的可观察项.现在发生的是,当我选中或取消选中任何复选框时,Knockout会将其视为肮脏"项目,并启用了我不需要的保存"按钮.

I am using Knockout Js for my view page. I have a requirement where if any editable field changes, I have to enable Save button else not. This is working nicely. My issue is I have checkboxes too for each row of item. These are observable items in my viewModel. What happens now is when I check or uncheck any checkbox, Knockout considers that as Dirty item and enables the Save button which I don't want.

我该如何解决?

推荐答案

我不确定您用于脏标志的确切代码,但是如果涉及在

I am not sure of the exact code that you are using for a dirty flag, but if it involves using ko.toJS in a dependentObservable like this, then there is a trick that you can use to have it skip some observables.

如果您创建的可观察对象是函数的属性,则ko.toJS将找不到它.

If you create an observable that is a property of a function, then ko.toJS will not find it.

这是两个示例(someFlaganotherFlag):

function Item(id, name) {
    this.id = ko.observable(id);
    //create a sub-observable that the dirty flag won't find
    this.id.someFlag = ko.observable(false);
    this.name = ko.observable(name);
    this.dirtyFlag = new ko.dirtyFlag(this);

    //or similarly, place an observable on a plain ol' function
    this.forgetAboutMe = function() { };
    this.forgetAboutMe.anotherFlag = ko.observable(false);  
}

此处提供示例: http://jsfiddle.net/rniemeyer/vGU88/

这篇关于淘汰赛JS-脏旗问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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