user1593470
user1593470

Reputation: 11

Simple Knockout sample code does not work on jsfiddle

I am new to both knockout and using jsfiddle. Please let me know why this sample code does not run on jsfiddle.

jsfiddle sample

Upvotes: 1

Views: 897

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134581

You don't have valid javascript code so knockout wasn't able to do its magic. Also you were missing some critical things that makes it work.

Since you are binding to the name property, it must be set as an observable if you want to be able to see the changes.

name: ko.observable("knockout"),

Being an observable, you have to "call" it with your new value to change it. What you had in your changeName() function was just not a valid statement (you were in the body of the function, not the object literal).

changeName: function () {
    this.name("jsfiddle");
}

With those changes in place, it should work now.

http://jsfiddle.net/vwy7B/12/

Upvotes: 1

Related Questions