Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Created June 17, 2012 01:33
Show Gist options
  • Select an option

  • Save fiveisprime/2943088 to your computer and use it in GitHub Desktop.

Select an option

Save fiveisprime/2943088 to your computer and use it in GitHub Desktop.
Knockout example
<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>
$(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