Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Object JavaScript - Knockout using Mustache Templates
<title>ko.mustache.js example</title>
<!-- place for rendered template -->
<div data-bind='template: "productTemplate"'></div>
<!-- mustache template -->
<script id='productTemplate' type='text/html'>
{{ product }} has {{ quantity }}
<button data-bind='click: sellItem'>Sell Item</button>
<script type="text/javascript" src="Scripts/mustache.js"></script>
<script type="text/javascript" src="Scripts/knockout-2.2.1.js"></script>
<script type="text/javascript" src="Scripts/ko.mustache.js"></script>
ko.setTemplateEngine(new ko.mustacheTemplateEngine());
<!-- knockout model and bindings -->
<script type='text/javascript'>
var viewModel = {
product: 'Widet',
quantity: ko.observable(4),
sellItem: function () {
if(this.quantity() > 0) {
this.quantity(this.quantity() -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment