PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/javascript/shared-package/backbone-extensions.js

https://bitbucket.org/deds/mm-academygrove
JavaScript | 88 lines | 72 code | 8 blank | 8 comment | 12 complexity | d9acdb97d794455c35efafea22c2d116 MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-3.0, GPL-3.0, MIT
  1. // This file contains extensions to Backbone models, collections, etc. that can
  2. // be reused throughout the codebase, as well as any global overrides.
  3. /* Backbone.Collection has no facility to asynchronously fetch individual
  4. models. IncrementalCollection.fetchByID() will return the given model
  5. if it is already loaded from the server, or fetch it immediately. The
  6. callback is called either immediately or when the model is finished
  7. loading. The __inited flag on the model is set to true if the model
  8. has been loaded from the server. */
  9. window.IncrementalCollection = Backbone.Collection.extend({
  10. fetchByID: function(id, callback, args) {
  11. var ret = this.get(id);
  12. if (!ret) {
  13. if (!this.idAttribute) {
  14. this.idAttribute = this.model.prototype.idAttribute;
  15. if (!this.idAttribute) {
  16. this.idAttribute = "id";
  17. }
  18. }
  19. var attrs = {};
  20. attrs[this.idAttribute] = id;
  21. ret = new this.model(attrs);
  22. this.add(ret, {silent: true});
  23. }
  24. if (!ret.__inited) {
  25. if (!ret.__callbacks) {
  26. ret.__callbacks = [];
  27. }
  28. if (callback) {
  29. ret.__callbacks.push({ callback: callback, args: args });
  30. }
  31. if (!ret.__requesting) {
  32. KAConsole.log("IC (" + id + "): Sending request...");
  33. ret.fetch({
  34. success: function() {
  35. KAConsole.log("IC (" + id + "): Request succeeded.");
  36. ret.__inited = true;
  37. ret.__requesting = false;
  38. _.each(ret.__callbacks, function(cb) {
  39. cb.callback.apply(null, [ret].concat(cb.args));
  40. });
  41. ret.__callbacks = [];
  42. },
  43. error: function() {
  44. KAConsole.log("IC (" + id + "): Request failed!");
  45. ret.__requesting = false;
  46. }
  47. });
  48. ret.__requesting = true;
  49. } else {
  50. KAConsole.log("IC (" + id + "): Already requested.");
  51. }
  52. } else {
  53. KAConsole.log("IC (" + id + "): Already loaded.");
  54. if (callback) {
  55. callback.apply(null, [ret].concat(args));
  56. }
  57. }
  58. return ret;
  59. },
  60. resetInited: function(models, options) {
  61. this.reset(models, options);
  62. _.each(this.models, function(model) {
  63. model.__inited = true;
  64. });
  65. },
  66. addInited: function(models, options) {
  67. if (!this.idAttribute) {
  68. this.idAttribute = new this.model({}).idAttribute;
  69. if (!this.idAttribute) {
  70. this.idAttribute = "id";
  71. }
  72. }
  73. var self = this;
  74. this.add(models, options);
  75. _.each(models, function(model) {
  76. var newModel = self.get(model[self.idAttribute]);
  77. newModel.__inited = true;
  78. });
  79. }
  80. });