Skip to content

Instantly share code, notes, and snippets.

@nikolaplejic
Last active December 24, 2015 23:39
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 nikolaplejic/6881702 to your computer and use it in GitHub Desktop.
Save nikolaplejic/6881702 to your computer and use it in GitHub Desktop.
Client-side GitHub repo search module.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>zgPHP GitHub snippet test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="githubreposearch.js"></script>
</head>
<body>
<dl id="github">
</dl>
<script>
jQuery.noConflict();
zgPHPGitHub = Object.create(GitHubRepoSearch);
zgPHPGitHub.init({
keyword: "zgphp",
container: "#github"
}).displayRepos();
</script>
</body>
</html>
/* jshint globalstrict: true, unused: false, laxbreak: true */
/* global jQuery:false, $:false, _:false, console: false */
var GitHubRepoSearch = (function($, _) {
"use strict";
var M = {
init: function(opts) {
this.options = $.extend({
keyword: "quux",
container: "#github-repo-search",
githubAPI: "https://api.github.com"
}, opts);
return this;
},
displayRepos: function() {
var T = this;
searchRepositories.call(this).done(function(data) {
renderRepositories.call(T, data.items);
}).fail(function(err) {
console.error("GitHupRepoSearch error: %o", err);
});
return this;
}
};
var searchRepositories = function() {
var endpoint = this.options.githubAPI + "/search/repositories?q=" +
encodeURIComponent(this.options.keyword);
return $.ajax({
type: "GET",
url: endpoint,
headers: {
"Accept": "application/vnd.github.preview"
}
});
};
var renderRepositories = function(data) {
var tmpl = _.template(getTemplate.call(this)),
T = this;
$.each(data, function(i) {
$(T.options.container).append(tmpl(this));
});
};
var getTemplate = function() {
return '<dt><a href="<%- url %>"><%- name %></a>, by <a href="<%= owner.html_url %>"><%- owner.login %></a></dt>' +
'<dd><%- description %></dd>';
};
return M;
})(jQuery, _);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment