淘汰赛-取消更改活动吗? [英] Knockout - Cancel a Change Event?

查看:69
本文介绍了淘汰赛-取消更改活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图模型上有一个绑定到可观察对象的复选框.我要求从本质上弹出您确定吗?"如果用户将其从true更改为false,则提示确认.我度过了可怕的时光,找出使更改可取消"的最佳位置. .

I have a checkbox bound to an observable on a view model. I have a requirement to essentially pop up an "Are you sure?" confirmation prompt if the user changes it from true to false. I'm having a horrible time figuring out the best place to make the change "cancelable" . . .

1)点击事件的jQuery处理程序 2)Viewmodel内部订阅"beforeChange" 3)Viewmodel内部订阅(正常)

1) jQuery handler for click event 2) Viewmodel internal subscribe "beforeChange" 3) Viewmodel internal subscribe (normal)

无论如何,我非常希望有机会直接取消更改,而不是对更改做出反应,并有可能在需要时将其恢复为以前的值.

In any case, I'd vastly prefer to have the chance to cancel the change outright than react to the change, potentially reverting it back to its previous value if need be.

淘汰赛的订阅活动是否使您有机会取消更改?任何见识将不胜感激.谢谢!

Does Knockout's subscribe event give you the chance to cancel a change? Any insight would be appreciated. Thanks!

推荐答案

使用jQuery的stopImmediatePropagation,这是一个简单的选项:

Here is a simple option using jQuery's stopImmediatePropagation:

http://jsfiddle.net/rniemeyer/cBvXM/

<input type="checkbox" data-bind="click: confirmCheck, checked: myvalue" />

js:

var viewModel = {
    myvalue: ko.observable(false),
    confirmCheck: function(data, event) {
        if (!confirm("Are you sure?")) {
            event.stopImmediatePropagation();            
            return false;
        }
        return true;
    }
};

这篇关于淘汰赛-取消更改活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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