Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ad37cc98bf
Fetching contributors…

Cannot retrieve contributors at this time

file 183 lines (164 sloc) 6.158 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
<html>
<head>
<style type="text/css">
div { height: 20px; }
table { border: black solid 1px; }
td, th { padding: 1px; text-align: center; }
</style>
</head>
<body>
<div data-bind="text: description">
</div>

<table data-bind="template: 'rowsTmpl', delegatedClick: [{callback: 'toggleShowDetails', selector: 'td a' }, { callback: deleteRow, selector: 'button' }]"></table>
<script id="rowsTmpl" type="text/html">
<thead data-bind="delegatedEvent: [{ event: 'mouseover', callback: setDescription, selector: 'th'}, { event: 'mouseout', callback: clearDescription, selector: 'th'}]">
<tr data-bind="template: { name: 'headTmpl', foreach: headers }"><th></th></tr>
</thead>
<tbody data-bind="template: { name: 'rowTmpl', foreach: rows }"></tbody>
</script>

<script id="rowTmpl" type="text/html">
<tr data-bind="template: { name: 'cellTmpl', foreach: cells }">
<td><button>Delete</button></td>
</tr>
</script>

<script id="headTmpl" type="text/html">
<th data-bind="text: name"></th>
</script>

<script id="cellTmpl" type="text/html">
<td>
<a href="javascript: void(0);" data-bind="text: name"></a>
<div data-bind="visible: showDetails, text: $data"></div>
</td>
</script>

<script type="text/javascript" src="Scripts/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="Scripts/jquery.tmpl.js"></script>
<script type="text/javascript" src="Scripts/knockout-latest.debug.js"></script>

<script type="text/javascript">
function Cell(name, x, y) {
this.name = name;
this.x = x;
this.y = y;
this.showDetails = ko.observable(false);
this.toggleShowDetails = function() {
this.showDetails(!this.showDetails());
}.bind(this);
}

Cell.prototype.toString = function() {
return "(" + this.x + "," + this.y + ")";
}

var viewModel = {
counter: 0,
headers: ko.observableArray([]),
rows: ko.observableArray([]),
deleteRow: function(row) {
viewModel.rows.remove(row);
},
setDescription: function(header) {
viewModel.description(header.description);
},
clearDescription: function() {
viewModel.description("");
},
description: ko.observable("")
};

//setup a 20x20 grid of cells
for (var i = 0; i < 15; i++) {
var row = {
name: i,
cells: []
};
for (var j = 0; j < 15; j++) {
row.cells.push(new Cell(viewModel.counter++, i, j));
}
viewModel.rows.push(row);
viewModel.headers.push({
name: i,
description: "This is a description for column " + i
});
}

//binding to do event delegation for any event
ko.bindingHandlers.delegatedEvent = {
init: function(element, valueAccessor, allBindings, viewModel) {
var eventsToHandle = valueAccessor() || {};
//if a single event was passed, then convert it to an array
if (!$.isArray(eventsToHandle)) {
eventsToHandle = [eventsToHandle];
}
ko.utils.arrayForEach(eventsToHandle, function(eventOptions) {
var realCallback = function(event) {
var element = event.target;
var options = eventOptions;
//verify that the element matches our selector
if ($(element).is(options.selector)) {
//get real context
var context = $(event.target).tmplItem().data;
//if a string was passed for the function, then assume it is a function of the real context
if (typeof options.callback === "string" && typeof context[options.callback] === "function") {
return context[options.callback].call(context, event);
}
//if a function was passed, then give it the real context as a param
return options.callback.call(viewModel, context, event);
}
}

var realValueAccessor = function() {
var result = {};
result[eventOptions.event] = realCallback;
return result;
}

ko.bindingHandlers.event.init(element, realValueAccessor, allBindings, viewModel);
});
}
};

//binding to do event delegation for click events
ko.bindingHandlers.delegatedClick = {
init: function(element, valueAccessor, allBindings, viewModel) {
var clicksToHandle = valueAccessor();
//if a single event was passed, then convert it to an array
if (!$.isArray(clicksToHandle)) {
clicksToHandle = [clicksHandle];
}
ko.utils.arrayMap(clicksToHandle, function(options) {
options.event = 'click';
return options;
});

var realValueAccessor = function() {
return clicksToHandle;
}

ko.bindingHandlers.delegatedEvent.init(element, realValueAccessor, allBindings, viewModel)
}
}

//binding to do event delegation for click events
ko.bindingHandlers.delegatedClickLong = {
init: function(element, valueAccessor, allBindings, viewModel) {
var clicksToHandle = valueAccessor();
//if a single event was passed, then convert it to an array
if (!$.isArray(clicksToHandle)) {
clicksToHandle = [clicksHandle];
}
ko.utils.arrayForEach(clicksToHandle, function(options) {
//build a function that will call our function with the correct context
var realValueAccessor = function() {
return function(event) {
var element = event.target;
//verify that the element matches our selector
if ($(element).is(options.selector)) {
//get real context
var context = $(event.target).tmplItem().data;
//if a string was passed for the function, then assume it is a function of the real context
if (typeof options.callback === "string" && typeof context[options.callback] === "function") {
return context[options.callback].call(context, event);
}
//if a function was passed, then give it the real context as a param
return options.callback.call(viewModel, context, event);
}
}
}

//call real click binding's init
ko.bindingHandlers.click.init(element, realValueAccessor, allBindings, viewModel);
});
}
}

ko.applyBindings(viewModel);

</script>
</body>
</html>
Something went wrong with that request. Please try again.