Skip to content

Instantly share code, notes, and snippets.

@4bu
Created December 13, 2011 15:44
Show Gist options
  • Save 4bu/1472597 to your computer and use it in GitHub Desktop.
Save 4bu/1472597 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.tmpl.js"></script>
<script src="ko.js"></script>
</head>
<body>
You have <span data-bind="text: wishes().length"></span> wishes.
<form data-bind="submit: add">
<input type="text" data-bind="value: enteredWish" placeholder="pleace add your wish"/>
<input type="submit" value="add" />
</form>
<ul data-bind="template: {name: 'wish-template', foreach: wishes}"></ul>
<script id="wish-template" type="text/html">
<li>
${ wish }
<input type="button" data-bind="value: 'remove', click: remove" />
</li>
</script>
<script>
var WishViewModel = function (wish) {
return {
wish: ko.observable(wish),
remove: function () {
WishFormViewModel.wishes.remove(this)
}
};
};
var WishFormViewModel = {
wishes: ko.observableArray(),
enteredWish: ko.observable(''),
add: function () {
if (this.enteredWish().trim() === '') {
return;
}
this.wishes.push(
new WishViewModel(this.enteredWish())
);
this.enteredWish('');
}
}
ko.applyBindings(WishFormViewModel);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment