Skip to content

Instantly share code, notes, and snippets.

@luisdalmolin
Created June 19, 2012 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisdalmolin/2951945 to your computer and use it in GitHub Desktop.
Save luisdalmolin/2951945 to your computer and use it in GitHub Desktop.
Carregando os gists por ajax
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Carregando GIST's por ajax</title>
<style type="text/css">
body {
font-family: Helvetica, sans-serif;
font-size: 13px;
margin: 10px; }
</style>
<link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.gist.js"></script>
</head>
<body>
<h1>Carregando Gist's por ajax</h1>
<div data-gist="2951945"></div>
<script type="text/javascript">
(function() {
$().loadGists();
})();
</script>
</body>
</html>
/*
Carregando os gists por ajax
Author: Luís Dalmolin <luis@escape.ppg.br>
*/
;(function ( $, window, undefined ) {
// carregando os gists
$.fn.loadGists = function( options )
{
// sobreescrevendo os defaults
var opt = $.extend( $.fn.loadGists.options, options );
$( opt.element ).each(function(){
var $this = $(this),
id = $this.attr('data-gist'),
url = 'https://gist.github.com/' + id + '.json';
// mensagem de load
$this.html('Carregando gist ' + url + ' ...');
// buscando o GIST
$.ajax({
url : url,
dataType : 'jsonp',
timeout : 10000,
success: function(response) {
if( response && response.div ) {
// adicionando o conteudo
$this.html( response.div );
}
},
error: function(){
$this.html('Erro ao carregar o GIST ' + url);
}
});
});
}
// defaults
$.fn.loadGists.options = {
element : '[data-gist]'
}
}(jQuery, window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment