如何从javascript触发Kendo可观察的更改事件?

时间:2015-10-22 20:31:06

标签: kendo-ui telerik kendo-observable

标题说明了一切。

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

感谢您的帮助。!!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

参加模特改变事件应该为你解决问题

$(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);
});