首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVVM-KendoNumericTextBox恢复以前的值而不触发“更改”两次

MVVM-KendoNumericTextBox恢复以前的值而不触发“更改”两次
EN

Stack Overflow用户
提问于 2015-07-22 15:12:25
回答 2查看 1.3K关注 0票数 0

我使用的是Kendo,并且我有一个绑定到kendo可观察到的kendo数字文本框。我想要的是:当用户更改值时,确认应该弹出,比如“您确定吗?”如果是的,->,没问题,继续吧。如果没有->,什么也不应该发生!

理论上听起来很简单,因为.但我发现了三个主要问题:

1)数字文本框只有两个事件:旋转和变化.因此,任何使用keypress/focus/或任何其他事件的想法都会被丢弃。

2)所以尝试使用变更事件..。但我不能preventDefault!另一种尝试是保存以前的值,并在“没有答案”的情况下恢复它,但是这会让我触发两次事件更改!

3)任何其他“观察”数字文本框的模型字段都会在我回答确认框之前更改.我绝对不想这样!

我也有一个下拉列表和一个数据报警器,必须以同样的方式工作!

救命啊!

提供了一个快速示例:在这里,您可以看到在用户回答yes/no (问题3)和keypress/ numericbox2 /numericbox2无法工作之前,numericbox2(正在观察numericbox1并被计算)是如何改变自己的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-29 10:24:37

我解决了一个定制绑定问题,它要求您在html小部件之间进行确认,更改->模型更新。

代码语言:javascript
复制
kendo.data.binders.widget.valueConfirm = kendo.data.Binder.extend({
            init: function (widget, bindings, options) { // start
                kendo.data.Binder.fn.init.call(this, widget.element[0], bindings, options);
                this.widget = widget;
                this._change = $.proxy(this.change, this);
                this.widget.bind("change", this._change); // observe
            },
            refresh: function () { // when model change
                if (!this._initChange) {
                    var widget = this.widget;
                    var value = this.bindings.valueConfirm.get(); // value of the model
                    if (widget.ns == ".kendoDropDownList") { // for the dropdown i have to use select
                        widget.select(function (d) {
                            return d.id == value.id;
                        });
                    }
                    else widget.value(value); // update widget
                }
            },
            change: function () { // when html item change
                var widget = this.widget;
                if (widget.ns == ".kendoDropDownList") var value = widget.dataItem(); // for dropdown i need dataitem
                else var value = widget.value();

                var old = this.bindings.valueConfirm.get();
                    this._initChange = true;
                    // I want to bypass the confirm if the value is not valid (for example after 1st load with blank values).
                    if (old == null || old == 'undefined' || old == 'NaN') this.bindings.valueConfirm.set(value); // Update the View-Model
                    else {
                        if (confirm("Are you sure?")) {
                            this.bindings.valueConfirm.set(value); // Update the View-Model
                        }
                        else {
                            this._initChange = false;
                            this.refresh(); // Reset old value
                        }
                    }
                this._initChange = false;
            },
            destroy: function () { // dunno if this is useful
                this.widget.unbind("change", this._change);
            }
        });
票数 0
EN

Stack Overflow用户

发布于 2015-07-24 12:51:08

下面是关于默认不支持的绑定事件的答案:Kendo MVVM and binding or extending custom events

对于preventDefault (或“恢复”值)。我试着按照您的建议存储以前的值,它不会触发两次:

代码语言:javascript
复制
var viewModel = kendo.observable({
    myItem: { 
      // fields, etc
      myNumericBox: 10,
      myNumericBox2: function () {
        return viewModel.get("myItem.myNumericBox")*2;
      },
      tmp: 10
    },
    onChange: function (e) {
        if ( confirm("are you sure?")) {
                viewModel.set("myItem.tmp", viewModel.get("myItem.myNumericBox"));
          }
          else {
              viewModel.set("myItem.myNumericBox", viewModel.get("myItem.tmp"));

          }
    },
  tryf: function () {
    alert("hello!"); // doesn't trigger
  },
  tryk: function() {
    alert("hello2!"); // doesn't trigger
  }

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31567171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档