|
1 | 1 | /*
|
2 |
| - * Project: jQuery Github Repos |
3 |
| - * Description: A jQuery plugin to display your Github Repositories. |
4 |
| - * Author: Zeno Rocha |
5 |
| - * License: MIT |
| 2 | + * jQuery Github Repos v0.3.1 |
| 3 | + * A jQuery plugin to display your Github Repositories. |
| 4 | + * http://git.io/3A1RMg |
| 5 | + * |
| 6 | + * Zeno Rocha |
| 7 | + * MIT License |
6 | 8 | */
|
7 | 9 |
|
8 | 10 | // the semi-colon before function invocation is a safety net against concatenated
|
|
30 | 32 | this.element = element;
|
31 | 33 | this.$container = $(element);
|
32 | 34 | this.repo = this.$container.attr("data-repo");
|
| 35 | + this.cached; |
33 | 36 |
|
34 | 37 | // jQuery has an extend method which merges the contents of two or
|
35 | 38 | // more objects, storing the result in the first object. The first object
|
|
45 | 48 |
|
46 | 49 | Plugin.prototype.init = function () {
|
47 | 50 |
|
48 |
| - var self = this; |
| 51 | + var self = this; |
49 | 52 |
|
50 | 53 | $.ajax({
|
51 | 54 | url: 'https://api.github.com/repos/' + this.repo,
|
52 | 55 | dataType: 'jsonp',
|
53 | 56 | success: function(results){
|
54 | 57 |
|
55 |
| - var repo = results.data; |
56 |
| - var date = new Date(repo.pushed_at); |
57 |
| - var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear(); |
58 |
| - var $widget = $(' \ |
59 |
| - <div class="github-box"> \ |
60 |
| - <div class="github-box-header"> \ |
61 |
| - <h3> \ |
62 |
| - <a href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \ |
63 |
| - </h3> \ |
64 |
| - <div class="github-stats"> \ |
65 |
| - <a class="repo-watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \ |
66 |
| - <a class="repo-forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \ |
67 |
| - </div> \ |
68 |
| - </div> \ |
69 |
| - <div class="github-box-content"> \ |
70 |
| - <p>' + repo.description + ' — <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \ |
71 |
| - </div> \ |
72 |
| - <div class="github-box-download"> \ |
73 |
| - <p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \ |
74 |
| - <a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \ |
75 |
| - </div> \ |
76 |
| - </div> \ |
77 |
| - '); |
78 |
| - |
79 |
| - $widget.appendTo(self.$container); |
| 58 | + // Handle API failures |
| 59 | + if (results.meta.status >= 400 && results.data.message){ |
| 60 | + console.warn(results.data.message); |
| 61 | + return; |
| 62 | + } |
| 63 | + else { |
| 64 | + self.applyTemplate(results.data); |
| 65 | + } |
80 | 66 |
|
81 | 67 | }
|
82 |
| - |
83 | 68 | });
|
| 69 | + |
| 70 | + }; |
| 71 | + |
| 72 | + Plugin.prototype.applyTemplate = function (repo) { |
| 73 | + |
| 74 | + var self = this; |
| 75 | + |
| 76 | + var date = new Date(repo.pushed_at); |
| 77 | + var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear(); |
| 78 | + |
| 79 | + var $widget = $(' \ |
| 80 | + <div class="github-box"> \ |
| 81 | + <div class="github-box-header"> \ |
| 82 | + <h3> \ |
| 83 | + <a href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \ |
| 84 | + </h3> \ |
| 85 | + <div class="github-stats"> \ |
| 86 | + <a class="repo-watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \ |
| 87 | + <a class="repo-forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \ |
| 88 | + </div> \ |
| 89 | + </div> \ |
| 90 | + <div class="github-box-content"> \ |
| 91 | + <p>' + repo.description + ' — <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \ |
| 92 | + </div> \ |
| 93 | + <div class="github-box-download"> \ |
| 94 | + <p class="repo-update">Latest commit to <strong>master</strong> on ' + repo.pushed_at + '</p> \ |
| 95 | + <a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \ |
| 96 | + </div> \ |
| 97 | + </div> \ |
| 98 | + '); |
| 99 | + |
| 100 | + // console.log($widget); |
| 101 | + self.appendTemplate($widget); |
| 102 | + |
| 103 | + }; |
| 104 | + |
| 105 | + Plugin.prototype.appendTemplate = function ($widget) { |
| 106 | + var self = this; |
| 107 | + $widget.appendTo(self.$container); |
84 | 108 | };
|
85 | 109 |
|
86 | 110 | // A really lightweight plugin wrapper around the constructor,
|
|
0 commit comments