Knockoutjs.com - Hello World Example
http://knockoutjs.com/examples/helloWorld.html
by Bryan Dellinger
HTML
<script src="http://knockoutjs.com/downloads/knockout-3.2.0.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/js/jasny-bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<table class="table table-striped">
<thead>
<tr>
<th>Category</th>
<th>Name</th>
<th>Get Info</th>
</tr>
</thead>
<tbody data-bind="foreach: tehlist">
<tr>
<td>
<label data-bind="text:category" </td>
<td>
<label data-bind="text:name" </td>
<td>
<button type="button" class="btn btn-lg btn-danger" data-toggle="popover" data-bind="popover: getInfo, popoverTitle: category">?</button>
</td>
</tr>
</tbody>
</table>
<div>
JavaScript
ko.bindingHandlers.popover = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
ko.bindingHandlers.value.init(element, valueAccessor, allBindings);
var source = allBindings.get('popoverTitle');
var sourceUnwrapped = ko.unwrap(source);
$(element).popover({
trigger: 'focus',
content: valueAccessor(),
title: sourceUnwrapped
});
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = valueAccessor();
ko.bindingHandlers.value.update(element, valueAccessor);
}
};
function teh(category, name) {
var self = this;
this.category = ko.observable(category);
this.name = ko.observable(name);
this.getInfo = ko.computed(function() {
return this.name() + " " + Math.random()
}, this);
}
function model() {
var self = this;
this.tehlist = ko.observableArray('');
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
mymodel.tehlist.push(new teh('car', 'honda'));
mymodel.tehlist.push(new teh('dog', 'pug'));
mymodel.tehlist.push(new teh('language', 'spanish'));
mymodel.tehlist.push(new teh('pc', 'dell'));
});