123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @api
- */
- define([
- 'jquery',
- 'underscore',
- 'mageUtils',
- 'rjsResolver',
- 'uiLayout',
- 'Magento_Ui/js/modal/alert',
- 'mage/translate',
- 'uiElement'
- ], function ($, _, utils, resolver, layout, alert, $t, Element) {
- 'use strict';
- return Element.extend({
- defaults: {
- firstLoad: true,
- lastError: false,
- storageConfig: {
- component: 'Magento_Ui/js/grid/data-storage',
- provider: '${ $.storageConfig.name }',
- name: '${ $.name }_storage',
- updateUrl: '${ $.update_url }'
- },
- listens: {
- params: 'onParamsChange',
- requestConfig: 'updateRequestConfig'
- },
- ignoreTmpls: {
- data: true
- }
- },
- /**
- * Initializes provider component.
- *
- * @returns {Provider} Chainable.
- */
- initialize: function () {
- utils.limit(this, 'onParamsChange', 5);
- _.bindAll(this, 'onReload');
- this._super()
- .initStorage()
- .clearData();
- // Load data when there will
- // be no more pending assets.
- resolver(this.reload, this);
- return this;
- },
- /**
- * Initializes storage component.
- *
- * @returns {Provider} Chainable.
- */
- initStorage: function () {
- layout([this.storageConfig]);
- return this;
- },
- /**
- * Clears provider's data properties.
- *
- * @returns {Provider} Chainable.
- */
- clearData: function () {
- this.setData({
- items: [],
- totalRecords: 0
- });
- return this;
- },
- /**
- * Overrides current data with a provided one.
- *
- * @param {Object} data - New data object.
- * @returns {Provider} Chainable.
- */
- setData: function (data) {
- data = this.processData(data);
- this.set('data', data);
- return this;
- },
- /**
- * Processes data before applying it.
- *
- * @param {Object} data - Data to be processed.
- * @returns {Object}
- */
- processData: function (data) {
- var items = data.items;
- _.each(items, function (record, index) {
- record._rowIndex = index;
- });
- return data;
- },
- /**
- * Reloads data with current parameters.
- *
- * @returns {Promise} Reload promise object.
- */
- reload: function (options) {
- var request = this.storage().getData(this.params, options);
- this.trigger('reload');
- request
- .done(this.onReload)
- .fail(this.onError.bind(this));
- return request;
- },
- /**
- * Handles changes of 'params' object.
- */
- onParamsChange: function () {
- // It's necessary to make a reload only
- // after the initial loading has been made.
- if (!this.firstLoad) {
- this.reload();
- }
- },
- /**
- * Handles reload error.
- */
- onError: function (xhr) {
- if (xhr.statusText === 'abort') {
- return;
- }
- this.set('lastError', true);
- this.firstLoad = false;
- alert({
- content: $t('Something went wrong.')
- });
- },
- /**
- * Handles successful data reload.
- *
- * @param {Object} data - Retrieved data object.
- */
- onReload: function (data) {
- this.firstLoad = false;
- this.set('lastError', false);
- this.setData(data)
- .trigger('reloaded');
- },
- /**
- * Updates storage's request configuration
- *
- * @param {Object} requestConfig
- */
- updateRequestConfig: function (requestConfig) {
- if (this.storage()) {
- _.extend(this.storage().requestConfig, requestConfig);
- }
- }
- });
- });
|