PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/dasher/javascript/adapters/sandboxes_adapter.js

https://gitlab.com/wilane/heka
JavaScript | 122 lines | 59 code | 15 blank | 48 comment | 4 complexity | 4edc1862f0b52f52b3686935390bb4c3 MD5 | raw file
  1. define(
  2. [
  3. "jquery",
  4. "underscore",
  5. "backbone",
  6. "adapters/base_adapter",
  7. "models/sandbox",
  8. "models/sandbox_output"
  9. ],
  10. function($, _, Backbone, BaseAdapter, Sandbox, SandboxOutput) {
  11. "use strict";
  12. /**
  13. * Adapter for retrieving sandboxes from the server.
  14. *
  15. * Consumes `/data/sandboxes.json`.
  16. *
  17. * @class SandboxesAdapter
  18. * @extends BaseAdapter
  19. *
  20. * @constructor
  21. */
  22. var Sandboxes = Backbone.Collection.extend({
  23. model: Sandbox,
  24. comparator: function(collection) {
  25. return(collection.get('Name'));
  26. }
  27. });
  28. var SandboxesAdapter = function() {
  29. /**
  30. * Sandboxes collection to be filled with data.
  31. *
  32. * @property {Backbone.Collection} sandboxes
  33. */
  34. this.sandboxes = new Sandboxes();
  35. };
  36. /**
  37. * Gets singleton instance of `SandboxesAdapter`
  38. *
  39. * @method instance
  40. *
  41. * @static
  42. */
  43. SandboxesAdapter.instance = function() {
  44. if (!this._instance) {
  45. this._instance = new SandboxesAdapter();
  46. }
  47. return this._instance;
  48. };
  49. _.extend(SandboxesAdapter.prototype, new BaseAdapter(), {
  50. /**
  51. * Finds sandbox asynchronously.
  52. *
  53. * @method findSandboxWhere
  54. *
  55. * @param {Object} options Search options that are passed options that are passed to `Backbone.Collection.findWhere`
  56. * @param {Function} callback Function called when find is complete
  57. * @param {String} callback.result Model found from the search.
  58. */
  59. findSandboxWhere: function(options, callback) {
  60. this.findWhere(this.sandboxes, options, callback);
  61. },
  62. /**
  63. * Fills sandboxes with data fetched from the server. Polls the server for updates after
  64. * fetching data.
  65. *
  66. * @method fill
  67. */
  68. fill: function(enablePolling) {
  69. var deferred = $.Deferred();
  70. this.fetch("data/sandboxes.json", function(response) {
  71. this.parseArrayIntoCollection(response.sandboxes, this.sandboxes);
  72. deferred.resolve(this.sandboxes);
  73. if (enablePolling) {
  74. this.pollForUpdates();
  75. }
  76. }.bind(this));
  77. return deferred;
  78. },
  79. /**
  80. * Parses array returned from the server into a collection.
  81. *
  82. * @method parseArrayIntoCollection
  83. * @param {Object[]} array Array to be parsed
  84. * @param {Backbone.Collection} collection Collection to be filled from parsed array
  85. */
  86. parseArrayIntoCollection: function(array, collection) {
  87. var sandboxes = _.collect(array, function(s) {
  88. // Add sandbox name to output for convenience
  89. _.each(s.Outputs, function(o) {
  90. o.SandboxName = s.Name;
  91. });
  92. var outputs = new Backbone.Collection(s.Outputs, { model: SandboxOutput });
  93. // No id is provided but the name is unique so use it as the id
  94. return new Sandbox(_.extend(s, {id: s.Name, Outputs: outputs }));
  95. });
  96. // If the collection already has data then we're doing an update so use set. Otherwise call
  97. // reset so that the views are properly rendered.
  98. if (collection.length > 0) {
  99. collection.set(sandboxes);
  100. } else {
  101. collection.reset(sandboxes);
  102. }
  103. }
  104. });
  105. return SandboxesAdapter;
  106. }
  107. );