Created
December 16, 2014 19:17
-
-
Save devdays/4635955dc2eff7c75b01 to your computer and use it in GitHub Desktop.
Object JavaScript - Knockout using Mustache Templates
This file contains 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
<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