如何从 javascript 触发 Kendo 可观察到的变化事件?

How trigger Kendo observable change event from javascript?

标题说明了一切。

我已经 this kendo observable example 设置,这样当名字被更改时,姓氏也会更改以匹配它。但是,如果您更改 javascript 中的名称,该示例将不起作用(请参阅单击注册按钮的代码)。

感谢您的帮助!

查找模型更改事件应该可以为您解决问题

$(document).ready(function() {
    var viewModel = kendo.observable({
        firstName: "John",
        lastName: "Doe",
        genders: ["Male", "Female"],
        gender: "Male",
        agreed: false,
        confirmed: false,
        register: function(e) {
            this.set("firstName", "bobbyyyyy!!!");
            e.preventDefault();

            this.set("confirmed", true);
        },
        startOver: function() {
            this.set("confirmed", false);
            this.set("agreed", false);
            this.set("gender", "Male");
            this.set("firstName", "John");
            this.set("lastName", "Doe");
        },

      firstNameChangeEvent:function(e) {

        switch(e.field){
          case "firstName":
            this.set("lastName", this.get("firstName"));                 
            break;
        }
      }
    });

    viewModel.bind("change", viewModel.firstNameChangeEvent);

    kendo.bind($("#example"), viewModel);
});

如果从代码更改名字,则不会触发 firstNameChangeEvent。更好的方法:

viewModel.bind("change", function(e) {
    if(e.field == "firstName") {
      this.set("lastName", this.get("firstName"));  
    }
});