KnockoutJS : Rendering a twitter feed using HTML5
this is an example forked from Nicolas Gallagher's timeline lab integrating his nice css and html5 timeline with a twitter feed. And since I've been learning knockout.js and amplify.js, I decided to include how to use these libraries to acheive this goal. Really simple!
by mgrcic
HTML
<script src="https://raw.github.com/appendto/amplify/master/core/amplify.core.js"></script>
<script src="https://raw.github.com/appendto/amplify/master/request/amplify.request.js"></script>
<script src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.js"></script>
<script src="https://raw.github.com/SteveSanderson/knockout.mapping/master/knockout.mapping.js"></script>
<section class="comments">
<article class="comment">
<a class="comment-img" href="#non">
<img src="https://si0.twimg.com/profile_images/1228977399/bg2_normal.jpg" alt="" width="50" height="50">
</a>
<div class="comment-body">
<div class="text">
<p>Hello, this is an example forked from <a href="http://nicolasgallagher.com">Nicolas Gallagher's</a> <a href="http://jsfiddle.net/necolas/vhZds/embedded/result/">timeline lab</a> integrating his nice css+html5 timeline with a twitter feed. And since I've been learning knockout.js and amplify.js, I decided to include how to use these libraries to acheive this goal. Really simple! <button id="refresh">refresh</button></p>
</div>
<p class="attribution">by <a href="http://anexiledderryman.com">Shay Doherty</a> at 21:06, 31st Jan 2012</p>
</div>
</article>
</section>
<section class="comments" data-bind="foreach: results" >
<article class="comment">
<a class="comment-img" href="#non">
<img data-bind='attr: { src: profile_image_url }' alt="" width="50" height="50">
</a>
<div class="comment-body">
<div class="text">
<p data-bind="text: text"></p>
</div>
<p class="attribution">by <a href="#non"><i data-bind="text: from_user"></i></a> at <i data-bind="text:created_at"></i></p>
</div>
</article>
</section>
CSS
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
body {
width: 500px;
margin: 20px auto;
font: 16px/1.4 Arial, sans-serif;
background: #f3eee9;
}
p {
margin: 0 0 1em;
}
.comment {
overflow: hidden;
padding: 0 0 1em;
border-bottom: 1px solid #ddd;
margin: 0 0 1em;
*zoom: 1;
}
.comment-img {
float: left;
margin-right: 33px;
border-radius: 5px;
overflow: hidden;
}
.comment-img img {
display: block;
}
.comment-body {
overflow: hidden;
}
.comment .text {
padding: 10px;
border: 1px solid #e5e5e5;
border-radius: 5px;
background: #fff;
}
.comment .text p:last-child {
margin: 0;
}
.comment .attribution {
margin: 0.5em 0 0;
font-size: 14px;
color: #666;
}
/* Decoration */
.comments,
.comment {
position: relative;
}
.comments:before,
.comment:before,
.comment .text:before {
content: "";
position: absolute;
top: 0;
left: 65px;
}
.comments:before {
width: 3px;
top: -20px;
bottom: -20px;
background: rgba(0,0,0,0.1);
}
.comment:before {
width: 9px;
height: 9px;
border: 3px solid #fff;
border-radius: 100px;
margin: 16px 0 0 -6px;
box-shadow: 0 1px 1px rgba(0,0,0,0.2), inset 0 1px 1px rgba(0,0,0,0.1);
background: #ccc;
}
.comment:hover:before {
background: orange;
}
.comment .text:before {
top: 18px;
left: 78px;
width: 9px;
height: 9px;
border-width: 0 0 1px 1px;
border-style: solid;
border-color: #e5e5e5;
background: #fff;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
}
JavaScript
amplify.request.define("twitter-search", "ajax", {
url: "http://search.twitter.com/search.json",
dataType: "jsonp",
dataMap: {
term: "q"
}
});
$(document).ready(function() {
(function($) {
var viewModel;
amplify.request("twitter-search", "q=html5&rpp=5", function(data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
$("#refresh").click(function() {
amplify.request("twitter-search", "q=html5&rpp=5", function(data) {
ko.mapping.fromJS(data, viewModel);
});
});
})(jQuery);
});