PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/lib/backbone/collection.js

https://github.com/koraktor/travis-ci
JavaScript | 103 lines | 100 code | 1 blank | 2 comment | 21 complexity | 73825291cbed1078add180516a1a259d MD5 | raw file
Possible License(s): MIT
  1. Travis.Collections.Base = Backbone.Collection.extend({
  2. fetched: false,
  3. fetching: false,
  4. initialize: function() {
  5. Backbone.Collection.prototype.initialize.apply(this, arguments);
  6. _.bindAll(this, 'whenFetched', 'select', 'selectLast', 'selectLastBy', 'deselect', 'getOrFetchLast', 'getOrFetchLastBy', 'getBy');
  7. },
  8. fetch: function(options) {
  9. options = options || {};
  10. var collection = this;
  11. this.startFetching();
  12. return Backbone.Collection.prototype.fetch.call(this, {
  13. success: function() {
  14. if(options.success) options.success.apply(this, arguments);
  15. collection.finishFetching();
  16. }.bind(this),
  17. error: function(e) {
  18. if(options.error) options.error.apply(this, arguments);
  19. collection.finishFetching();
  20. }.bind(this)
  21. });
  22. },
  23. whenFetched: function(callback, options) {
  24. if(!this.fetched || this.fetching) {
  25. var _callback = function() { this.unbind('fetched', _callback); return callback(this, options); }.bind(this);
  26. this.bind('fetched', _callback);
  27. if(!this.fetching) this.fetch();
  28. } else {
  29. callback(this, options);
  30. }
  31. },
  32. selected: function() {
  33. return this.detect(function(element) { return element.selected; }) || this.first();
  34. },
  35. select: function(id) {
  36. this.getOrFetch(id, function(element) { if(element) element.select(); });
  37. },
  38. selectLast: function() {
  39. this.getOrFetchLast(function(element) { if(element) element.select(); })
  40. },
  41. selectLastBy: function(options) {
  42. this.getOrFetchLastBy(options, function(element) { if(element) element.select(); });
  43. },
  44. deselect: function() {
  45. var element = this.selected();
  46. if(element) element.deselect();
  47. },
  48. getOrFetchLast: function(callback) {
  49. if(this.length > 0) {
  50. callback(this.last());
  51. } else {
  52. this.fetch({ success: function() { callback(this.last()); }.bind(this) });
  53. }
  54. },
  55. getOrFetchLastBy: function(options, callback) {
  56. var element = this.getBy(options);
  57. if(element) {
  58. callback(element);
  59. } else {
  60. this.fetch({ success: function(collection) { callback(this.getBy(options)) }.bind(this) });
  61. }
  62. },
  63. getBy: function(options) {
  64. return this.detect(function(element) {
  65. return _.all(options, function(value, name) { return element.get(name) == value; })
  66. });
  67. },
  68. getOrFetch: function(id, callback) {
  69. var element = this.get(id);
  70. if(element) {
  71. callback(element);
  72. } else {
  73. var model = new this.model({ id: id, collection: this });
  74. model.fetch({
  75. success: function(model) {
  76. var model = new this.model(model.attributes, { collection: this });
  77. if(model.get('parent_id')) {
  78. this.getOrFetch(model.get('parent_id'), function(parent) {
  79. callback(parent.matrix.get(id));
  80. });
  81. } else {
  82. this.add(model, { silent: true });
  83. callback(model);
  84. }
  85. }.bind(this),
  86. error: function() {
  87. // console.log('could not retrieve model:')
  88. // console.log(arguments);
  89. }
  90. });
  91. }
  92. },
  93. startFetching: function() {
  94. this.fetching = true;
  95. this.trigger('fetch');
  96. },
  97. finishFetching: function() {
  98. this.trigger('fetched');
  99. this.fetched = true;
  100. this.fetching = false;
  101. },
  102. });