Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Object JavaScript - Knockout using Mustache Templates
<html>
<head>
<title>ko.mustache.js example</title>
</head>
<body>
<!-- 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>
<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>
<script>
ko.setTemplateEngine(new ko.mustacheTemplateEngine());
</script>
<!-- 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);
}
}
};
ko.applyBindings(viewModel);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment