/
provider.js
102 lines (89 loc) · 2.58 KB
/
provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'underscore',
'uiElement',
'./client',
'mageUtils'
], function (_, Element, Client, utils) {
'use strict';
return Element.extend({
defaults: {
clientConfig: {
urls: {
save: '${ $.submit_url }',
beforeSave: '${ $.validate_url }'
}
},
ignoreTmpls: {
data: true
}
},
/**
* Initializes provider component.
*
* @returns {Provider} Chainable.
*/
initialize: function () {
this._super()
.initClient();
return this;
},
/**
* Initializes client component.
*
* @returns {Provider} Chainable.
*/
initClient: function () {
this.client = new Client(this.clientConfig);
return this;
},
/**
* Saves currently available data.
*
* @param {Object} [options] - Addtitional request options.
* @returns {Provider} Chainable.
*/
save: function (options) {
var data = this.get('data');
this.client.save(data, options);
return this;
},
/**
* Update data that stored in provider.
*
* @param {Boolean} isProvider
* @param {Object} newData
* @param {Object} oldData
*
* @returns {Provider}
*/
updateConfig: function (isProvider, newData, oldData) {
if (isProvider === true) {
this.setData(oldData, newData, this);
}
return this;
},
/**
* Set data to provder based on current data.
*
* @param {Object} oldData
* @param {Object} newData
* @param {Provider} current
* @param {String} parentPath
*/
setData: function (oldData, newData, current, parentPath) {
/* eslint-disable eqeqeq */
_.each(newData, function (val, key) {
if (_.isObject(val) || _.isArray(val)) {
this.setData(oldData[key], val, current[key], utils.fullPath(parentPath, key));
} else if (val != oldData[key] && oldData[key] == current[key]) {
this.set(utils.fullPath(parentPath, key), val);
}
}, this);
/* eslint-enable eqeqeq */
}
});
});