Created
June 17, 2012 01:33
-
-
Save fiveisprime/2943088 to your computer and use it in GitHub Desktop.
Knockout example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div data-bind="with: people"> | |
| Important people: | |
| <label>First Name</label> | |
| <input type="text" data-bind="value: first" /> | |
| <label>Last Name</label></div> | |
| <input type="text" data-bind="value: last" /> | |
| <button data-bind="click: addPerson">Add Person</button> | |
| <button data-bind="click: remove">Remove Person</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function() { | |
| /** | |
| * A collection of people with helper functions to add and remove. | |
| */ | |
| function peopleModel() { | |
| /** | |
| * The collection of people for this view model. | |
| */ | |
| this.people = ko.observableArray([ | |
| { firstName: "Erich", lastName: "Gamma" }, | |
| { firstName: "Richard", lastName: "Helm" }, | |
| { firstName: "Ralph", lastName: "Johnson" }, | |
| { firstName: "John", lastName: "Vlissides" } | |
| ]); | |
| /** | |
| * Adds a new person using a generic first and last name. | |
| */ | |
| this.addPerson = function() { | |
| this.people.push({firstName: "New", lastName: "Person" }); | |
| }; | |
| this.remove = function() { | |
| this.people.pop(); | |
| }; | |
| }; | |
| /** | |
| * The main view model. | |
| */ | |
| function appViewModel() { | |
| this.people = new peopleModel(); | |
| }; | |
| // Apply the viewmodel binding. | |
| ko.applyBindings(new appViewModel()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment