PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/app/collections/repos.js

https://gitlab.com/gregtyka/prose
JavaScript | 64 lines | 50 code | 14 blank | 0 comment | 2 complexity | 159338dec944f994b34805e4b7d0216b MD5 | raw file
  1. var _ = require('underscore');
  2. var Backbone = require('backbone');
  3. var Repo = require('../models/repo');
  4. var auth = require('../config');
  5. var cookie = require('../cookie');
  6. var util = require('../util');
  7. module.exports = Backbone.Collection.extend({
  8. model: Repo,
  9. initialize: function(models, options) {
  10. _.bindAll(this);
  11. this.user = options.user;
  12. this.comparator = function(repo) {
  13. return -(new Date(repo.get('updated_at')).getTime());
  14. };
  15. },
  16. fetch: function(options) {
  17. options = _.clone(options) || {};
  18. var cb = options.success;
  19. var success = (function(res, statusText, xhr) {
  20. this.add(res);
  21. util.parseLinkHeader(xhr, {
  22. success: success,
  23. complete: cb
  24. });
  25. }).bind(this);
  26. Backbone.Collection.prototype.fetch.call(this, _.extend(options, {
  27. success: (function(model, res, options) {
  28. util.parseLinkHeader(options.xhr, {
  29. success: success,
  30. error: cb
  31. });
  32. }).bind(this)
  33. }));
  34. },
  35. url: function() {
  36. var id = cookie.get('id');
  37. var type = this.user.get('type');
  38. var path;
  39. switch(type) {
  40. case 'User':
  41. path = (id && this.user.get('id') === id) ? '/user' :
  42. ('/users/' + this.user.get('login'))
  43. break;
  44. case 'Organization':
  45. path = '/orgs/' + this.user.get('login');
  46. break;
  47. }
  48. return auth.api + path + '/repos?per_page=100';
  49. }
  50. });