PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/backbone-github.js

https://github.com/foresthz/backbone-github
JavaScript | 128 lines | 112 code | 16 blank | 0 comment | 6 complexity | 0cc7bf960abf6a813054e3bdd38e056d MD5 | raw file
  1. (function() {
  2. window.GitHub = {};
  3. GitHub.token = null;
  4. GitHub.authenticate = function(username, password, options) {
  5. var postData;
  6. postData = {};
  7. if (options.scope != null) postData.scope = options.scope;
  8. return $.ajax({
  9. url: "https://api.github.com/authorizations",
  10. contentType: 'application/json',
  11. dataType: 'json',
  12. type: 'POST',
  13. data: JSON.stringify(postData),
  14. headers: {
  15. 'Authorization': "Basic " + (btoa("" + username + ":" + password))
  16. },
  17. success: function(d, s, x) {
  18. GitHub.token = d.token;
  19. if (options.success != null) return options.success(d, s, x);
  20. },
  21. error: options.error
  22. });
  23. };
  24. GitHub.sync = function(method, model, options) {
  25. var extendedOptions;
  26. extendedOptions = _.extend({
  27. beforeSend: function(xhr) {
  28. xhr.setRequestHeader('Accept', 'application/vnd.github+json');
  29. if (GitHub.token) {
  30. return xhr.setRequestHeader('Authorization', "bearer " + GitHub.token);
  31. }
  32. }
  33. }, options);
  34. return Backbone.sync(method, model, extendedOptions);
  35. };
  36. GitHub.Model = Backbone.Model.extend({
  37. sync: GitHub.sync
  38. });
  39. GitHub.Collection = Backbone.Collection.extend({
  40. sync: GitHub.sync
  41. });
  42. GitHub.Relations = {
  43. ownedRepos: function(options) {
  44. var repos;
  45. repos = new GitHub.Repos;
  46. repos.url = typeof this.url === "function" ? this.url() : this.url;
  47. repos.url += "/repos";
  48. repos.fetch(options);
  49. return repos;
  50. },
  51. ownedOrgs: function(options) {
  52. var orgs;
  53. orgs = new GitHub.Organizations;
  54. orgs.url = typeof this.url === "function" ? this.url() : this.url;
  55. orgs.url += "/orgs";
  56. orgs.fetch(options);
  57. return orgs;
  58. }
  59. };
  60. GitHub.User = GitHub.Model.extend({
  61. urlRoot: 'https://api.github.com/users/',
  62. repos: GitHub.Relations.ownedRepos,
  63. organizations: GitHub.Relations.ownedOrgs
  64. }, {
  65. fetch: function(name, options) {
  66. var user;
  67. user = new GitHub.User({
  68. id: name
  69. });
  70. user.fetch(options);
  71. return user;
  72. }
  73. });
  74. GitHub.Organization = GitHub.Model.extend({
  75. urlRoot: 'https://api.github.com/orgs/',
  76. repos: GitHub.Relations.ownedRepos
  77. }, {
  78. fetch: function(name, options) {
  79. var org;
  80. org = new GitHub.Organization({
  81. id: name
  82. });
  83. org.fetch(options);
  84. return org;
  85. }
  86. });
  87. GitHub.Organizations = GitHub.Collection.extend({
  88. url: 'https://api.github.com/user/orgs',
  89. model: GitHub.Organization
  90. });
  91. GitHub.Repo = GitHub.Model.extend({
  92. url: function() {
  93. return this.get('url') || ("https://api.github.com/repos/" + (this.get('path')));
  94. }
  95. }, {
  96. fetch: function(owner, name, options) {
  97. var repo;
  98. repo = new GitHub.Repo({
  99. path: "" + owner + "/" + name
  100. });
  101. repo.fetch(options);
  102. return repo;
  103. }
  104. });
  105. GitHub.Repos = GitHub.Collection.extend({
  106. url: 'https://api.github.com/user/repos',
  107. model: GitHub.Repo
  108. });
  109. GitHub.currentUser = new GitHub.User();
  110. GitHub.currentUser.url = "https://api.github.com/user";
  111. GitHub.currentUser.urlRoot = null;
  112. }).call(this);