provider.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'jquery',
  10. 'underscore',
  11. 'mageUtils',
  12. 'rjsResolver',
  13. 'uiLayout',
  14. 'Magento_Ui/js/modal/alert',
  15. 'mage/translate',
  16. 'uiElement'
  17. ], function ($, _, utils, resolver, layout, alert, $t, Element) {
  18. 'use strict';
  19. return Element.extend({
  20. defaults: {
  21. firstLoad: true,
  22. lastError: false,
  23. storageConfig: {
  24. component: 'Magento_Ui/js/grid/data-storage',
  25. provider: '${ $.storageConfig.name }',
  26. name: '${ $.name }_storage',
  27. updateUrl: '${ $.update_url }'
  28. },
  29. listens: {
  30. params: 'onParamsChange',
  31. requestConfig: 'updateRequestConfig'
  32. },
  33. ignoreTmpls: {
  34. data: true
  35. }
  36. },
  37. /**
  38. * Initializes provider component.
  39. *
  40. * @returns {Provider} Chainable.
  41. */
  42. initialize: function () {
  43. utils.limit(this, 'onParamsChange', 5);
  44. _.bindAll(this, 'onReload');
  45. this._super()
  46. .initStorage()
  47. .clearData();
  48. // Load data when there will
  49. // be no more pending assets.
  50. resolver(this.reload, this);
  51. return this;
  52. },
  53. /**
  54. * Initializes storage component.
  55. *
  56. * @returns {Provider} Chainable.
  57. */
  58. initStorage: function () {
  59. layout([this.storageConfig]);
  60. return this;
  61. },
  62. /**
  63. * Clears provider's data properties.
  64. *
  65. * @returns {Provider} Chainable.
  66. */
  67. clearData: function () {
  68. this.setData({
  69. items: [],
  70. totalRecords: 0
  71. });
  72. return this;
  73. },
  74. /**
  75. * Overrides current data with a provided one.
  76. *
  77. * @param {Object} data - New data object.
  78. * @returns {Provider} Chainable.
  79. */
  80. setData: function (data) {
  81. data = this.processData(data);
  82. this.set('data', data);
  83. return this;
  84. },
  85. /**
  86. * Processes data before applying it.
  87. *
  88. * @param {Object} data - Data to be processed.
  89. * @returns {Object}
  90. */
  91. processData: function (data) {
  92. var items = data.items;
  93. _.each(items, function (record, index) {
  94. record._rowIndex = index;
  95. });
  96. return data;
  97. },
  98. /**
  99. * Reloads data with current parameters.
  100. *
  101. * @returns {Promise} Reload promise object.
  102. */
  103. reload: function (options) {
  104. var request = this.storage().getData(this.params, options);
  105. this.trigger('reload');
  106. request
  107. .done(this.onReload)
  108. .fail(this.onError.bind(this));
  109. return request;
  110. },
  111. /**
  112. * Handles changes of 'params' object.
  113. */
  114. onParamsChange: function () {
  115. // It's necessary to make a reload only
  116. // after the initial loading has been made.
  117. if (!this.firstLoad) {
  118. this.reload();
  119. }
  120. },
  121. /**
  122. * Handles reload error.
  123. */
  124. onError: function (xhr) {
  125. if (xhr.statusText === 'abort') {
  126. return;
  127. }
  128. this.set('lastError', true);
  129. this.firstLoad = false;
  130. alert({
  131. content: $t('Something went wrong.')
  132. });
  133. },
  134. /**
  135. * Handles successful data reload.
  136. *
  137. * @param {Object} data - Retrieved data object.
  138. */
  139. onReload: function (data) {
  140. this.firstLoad = false;
  141. this.set('lastError', false);
  142. this.setData(data)
  143. .trigger('reloaded');
  144. },
  145. /**
  146. * Updates storage's request configuration
  147. *
  148. * @param {Object} requestConfig
  149. */
  150. updateRequestConfig: function (requestConfig) {
  151. if (this.storage()) {
  152. _.extend(this.storage().requestConfig, requestConfig);
  153. }
  154. }
  155. });
  156. });