Simple Twitter App with jQuery Templates and JSONP
I’ve been trying to come up with some cool applications for the jQuery Templates Plugin. So Twitter hosts this cool jsonp interface that enables you to pull down all kinds of twitter data to your client application – jsonp enables us to get around XSS, so the application doesn’t need a server-side component at all!!!
Check out the code:
<form id="twitter_search" >
<label for="twitter_search" >Search Twitter Feed:</label>
<input type="text" name="q" value="GUICenter" />
<input type="submit" name="submit" value="Submit" />
</form>
<div id="twitter_results_tmpl" style="display:none" >
<h4>Tweets from {name}</h4>
<ul>
<li style="display:none" >{text} <br/ >Created At: {created_at}</li>
</ul>
</div>
Twitter = {
init: function()
{
$('#twitter_search').submit(Twitter.search);
},
search: function()
{
$.ajax({
'url': 'http://twitter.com/statuses/user_timeline/'+$(this.q).val()+'.json',
'data': { count: 5 },
'success': Twitter.onSearchResult,
'dataType': 'jsonp'
});
return false;
},
onSearchResult: function(res)
{
$('#twitter_results_tmpl')
.render(res[0]['user'],{clone:true})
.removeAttr('id')
.find('.twitter_tmpl')
.render( res, {clone:true} )
.show().end().end().show();
}
};
$(document).ready( Twitter.init );
Try it out…
Cool, eh?