ServerCollection.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../lib/@waca/dashboard-common/dist/core/Collection', '../../lib/@waca/core-client/js/core-client/errors/BaseError'], function (Collection, BaseError) {
  8. /**
  9. * A collection which is created from the server. For example, if we had a server collection
  10. * called ThingsCollection and it specified a URL of /things then if we did:
  11. *
  12. * new ThingsCollection().fetch();
  13. *
  14. * Then that collection would make a call to:
  15. *
  16. * GET /things
  17. *
  18. * And materialize the collection from the results of that call.
  19. *
  20. * Overriders of this class should define a url property, e.g:
  21. *
  22. * ThingsCollection = ServerCollection.extend({
  23. *
  24. * url: '/things',
  25. * init: function() {
  26. * ...
  27. * },
  28. *
  29. * ...
  30. * });
  31. *
  32. */
  33. var ServerCollection = Collection.extend({
  34. init: function init(models, options) {
  35. ServerCollection.inherited('init', this, arguments);
  36. this._ajaxSvc = options.ajaxSvc;
  37. },
  38. /**
  39. * Get all the models in the collection from the server. Returns a promise which
  40. * is resolved when the collection has been reset with the result from the server.
  41. */
  42. fetch: function fetch() {
  43. var _this = this;
  44. return new Promise(function (resolve, reject) {
  45. _this._ajaxSvc.ajax({
  46. url: _this.url,
  47. type: 'GET',
  48. cache: false,
  49. headers: {
  50. Accept: 'application/json'
  51. }
  52. }).then(function (models) {
  53. _this.reset(models);
  54. resolve(models);
  55. }).catch(function (error) {
  56. reject(new BaseError(error));
  57. });
  58. });
  59. },
  60. /**
  61. * Fetch a model with the given ID and add it to the collection
  62. */
  63. fetchModel: function fetchModel(id) {
  64. var _this2 = this;
  65. return new Promise(function (resolve, reject) {
  66. _this2._ajaxSvc.ajax({
  67. url: _this2.url + '/' + id,
  68. type: 'GET',
  69. headers: { Accept: 'application/json' }
  70. }).then(function (model) {
  71. if (model) {
  72. _this2.add(model);
  73. }
  74. resolve(_this2.get(model));
  75. }).catch(function (error) {
  76. reject(new BaseError(error));
  77. });
  78. });
  79. },
  80. /**
  81. * Adds a new model to the collection by POSTing it to the collection.
  82. *
  83. */
  84. addModel: function addModel(model) {
  85. var _this3 = this;
  86. return new Promise(function (resolve, reject) {
  87. _this3._ajaxSvc.ajax({
  88. url: _this3.url,
  89. type: 'POST',
  90. contentType: 'application/json',
  91. processData: false,
  92. dataType: 'json',
  93. data: JSON.stringify(model),
  94. headers: { Accept: 'application/json' }
  95. }).then(function (serverModel) {
  96. if (serverModel) {
  97. _this3.add(serverModel);
  98. }
  99. resolve(_this3.get(serverModel));
  100. }).catch(function (error) {
  101. reject(new BaseError(error));
  102. });
  103. });
  104. }
  105. });
  106. return ServerCollection;
  107. });
  108. //# sourceMappingURL=ServerCollection.js.map