Skip to content

Instantly share code, notes, and snippets.

@jbpotonnier
Last active December 26, 2015 08:59
Show Gist options
  • Save jbpotonnier/7126670 to your computer and use it in GitHub Desktop.
Save jbpotonnier/7126670 to your computer and use it in GitHub Desktop.
Fun with Knockout and ajax calls. Display and update the last events on Github.
var events = ko.mapping.fromJS([]);
ko.applyBindings({
events: events,
onRowClicked: function (row) {
// modify model to update UI. Will be re-updated by next ajax call :P
row.actor.login("FOO");
}
});
setInterval(function () {
$.getJSON('https://api.github.com/events', function (data) {
ko.mapping.fromJS(data, events);
});
}
, 5000);
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Github Events</title>
<link type="text/css" rel="stylesheet" href="style.css">
<script type='text/javascript' src='lib/zepto.min.js'></script>
<script type='text/javascript' src='lib/knockout-2.3.0.js'></script>
<script type='text/javascript' src='lib/knockout.mapping-latest.js'></script>
</head>
<body>
<table>
<thead>
<tr>
<th>Type</th>
<th>Created At</th>
<th>By</th>
<th>Avatar</th>
<th>On</th>
</tr>
</thead>
<tbody data-bind="foreach: events">
<tr data-bind="click: $parent.onRowClicked">
<td data-bind="text: type"></td>
<td data-bind="text: created_at"></td>
<td data-bind="text: actor.login"></td>
<td><img width="80" height="80" data-bind="attr: {src: actor.avatar_url}"></td>
<td data-bind="text: repo.name"></td>
</tr>
</tbody>
</table>
<script type='text/javascript' src='events.js'></script>
</body>
</html>
body {
border: 1px solid #DFDFDF;
background-color: #F9F9F9;
border-radius: 3px;
font-family: Arial,"Bitstream Vera Sans",Helvetica,Verdana,sans-serif;
color: #333;
}
tr:hover { background: #d3d3d3; }
td, th {
border-top-color: white;
border-bottom: 1px solid #DFDFDF;
color: #555;
}
th {
font-family: Georgia,"Times New Roman","Bitstream Charter",Times,serif;
font-weight: normal;
padding: 7px 7px 8px;
text-align: left;
line-height: 1.3em;
font-size: 14px;
}
td {
font-size: 12px;
padding: 4px 7px 2px;
vertical-align: top;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment