RayLoveless
RayLoveless

Reputation: 21058

How trigger Kendo observable change event from javascript?

Title says it all.

I have this kendo observable example set up so that when the first name gets changed the last name will be changed to match it. However the example doesn't work if you change the name in javascript( see the code for Clicking on the registration button).

Thanks for any help.!!

Upvotes: 1

Views: 3625

Answers (2)

softawareblog.com
softawareblog.com

Reputation: 990

firstNameChangeEvent will not get fired if firstname is changed from code. The better way:

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

Upvotes: 1

JFM
JFM

Reputation: 753

inding to the models change event should fix things for you

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

Upvotes: 0

Related Questions