forked from Nemo64/hateoas-ajax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhateoas-ajax.html
297 lines (245 loc) · 9.56 KB
/
hateoas-ajax.html
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="./iron-ajax-behavior.html"/>
<!--
An extension of polymer's <a href="https://elements.polymer-project.org/elements/iron-ajax">iron-ajax</a> but with an addition of some magic related to <a href="http://en.wikipedia.org/wiki/HATEOAS">hateoas</a>.
<a href="http://stateless.co/hal_specification.html">HAL - Hypertext Application Language</a><br/>
Example:
<hateoas-ajax url="{{url}}" response="{{response}}"></hateoas-ajax>
Simply call relations like they are properties,
they are requested and through data binding automatically displayed when present.
<div>{{person.address}}</div>
-->
<dom-module id="hateoas-ajax">
<script>
Polymer({
is: 'hateoas-ajax',
// we want iron ajax behavior
behaviors: [Polymer.IronAjaxBehavior],
properties: {
/**
* The name of the links property.
*/
linkProperty: {
type: String,
value: '_links'
},
/**
* The name of the embedded property.
*/
embeddedProperty: {
type: String,
value: '_embedded'
}
},
observers: ['_process(lastResponse)'],
/**
* Creates a sub instance of this element for linked resoures.
*/
_createSubInstance: function() {
var request = document.createElement(this.nodeName);
request.linkProperty = this.linkProperty;
request.embeddedProperty = this.embeddedProperty;
request.headers = this.headers;
request.auto = false;
return request;
},
/**
* Process the raw response and extract embedded resources, create sub instances for linked resources etc.
*
* @param {Object} value - response data to process.
* @param {String} objectPath - this would be undefined for root response.
*/
_process: function(value, objectPath) {
if (!objectPath) {
objectPath = 'lastResponse';
}
if (value instanceof Array) {
value.forEach(function(object, index) {
this._modifyObject(object, objectPath + '.' + index);
}, this);
} else if (value instanceof Object) {
this._modifyObject(value, objectPath);
}
},
/**
* Process each iteration of a response, e.g: when fetching users, this method is called for each user inside users array.
*
* @param {Object} object
* @param {String} objectPath
*/
_modifyObject: function(object, objectPath) {
this._processEmbeddedProperties(object, objectPath);
this._processLinkProperties(object, objectPath);
},
/**
* Hides special properties within the raw response, such as embedded and links
*
* @param {Object} object
* @param {String} objectPath
*/
_hideProperty: function(object, property) {
var value = object[property];
delete object[property];
Object.defineProperty(object, property, {
enumerable: false,
configurable: false,
value: value
});
},
/**
* Process embedded properties. Collection of resources are wrapped within an embedded property.
*
* @param {Object} object
* @param {String} objectPath
*/
_processEmbeddedProperties: function(object, objectPath) {
var embedded = object[this.embeddedProperty];
if (!(embedded instanceof Object)) { return; }
// hide the property as it is not really a value
this._hideProperty(object, this.embeddedProperty);
Object.keys(embedded).forEach(function(embeddedProperty) {
var propertyPath = objectPath + '.' + embeddedProperty;
// don't overwrite anything
if (embeddedProperty in object) { return; }
Object.defineProperty(object, embeddedProperty, {
enumerable: false,
configurable: true,
value: embedded[embeddedProperty]
});
// modify the object as if it were a response
this._process(embedded[embeddedProperty], propertyPath);
this.notifyPath(propertyPath, embedded[embeddedProperty]);
}, this);
},
_processLinkProperties: function(object, objectPath) {
var links = object[this.linkProperty];
if (!(links instanceof Object)) { return; }
// hide the property as it is not really a value
this._hideProperty(object, this.linkProperty);
Object.keys(links).forEach(function(linkProperty) {
// don't overwrite anything
if (linkProperty in object) { return; }
var attributes = links[linkProperty];
this._createLinkGetMethod(object, objectPath, linkProperty, attributes);
this._createLinkPutMethod(object, objectPath, linkProperty, attributes);
this._createLinkPostMethod(object, objectPath, linkProperty, attributes);
this._createLinkDeleteMethod(object, objectPath, linkProperty, attributes);
}, this);
},
_attachHateoasElement: function(object, objectPath, propertyName) {
var ajax = this._createSubInstance();
var ajaxStorageProperty = propertyName + 'Handler';
Object.defineProperty(object, ajaxStorageProperty, {
enumerable: false,
configurable: false,
writable: false,
value: ajax
});
this.notifyPath(objectPath + '.' + ajaxStorageProperty, ajax);
return ajax;
},
_hiddenPropertyForward: function(hatoasAjax, fromProperty, targetObjectPath, toProperty, initialize) {
var targetObject = this.get(targetObjectPath);
var targetPropertyPath = targetObjectPath + '.' + toProperty;
var self = this;
var replaceValue = function(newValue) {
Object.defineProperty(targetObject, toProperty, {
enumerable: false,
configurable: true,
writable: true,
value: newValue
});
self.notifyPath(targetPropertyPath, newValue);
};
if (initialize) {
replaceValue(hatoasAjax[fromProperty]);
}
var dashCaseFromProperty = Polymer.CaseMap.camelToDashCase(fromProperty);
hatoasAjax.addEventListener(dashCaseFromProperty + '-changed', function(e) {
if (!e.detail.path || e.detail.path === fromProperty) {
// console.log(targetPropertyPath, e.detail.value);
replaceValue(e.detail.value);
} else {
// just forward changes that are within the property
var path = e.detail.path.replace(fromProperty, targetPropertyPath);
// console.log(path, e.detail.value);
self.notifyPath(path, e.detail.value);
}
});
},
_createLinkGetMethod: function(object, objectPath, propertyName, attributes) {
var ajax = this._attachHateoasElement(object, objectPath, propertyName);
ajax.url = attributes.href;
// forward lastResponse to the property we define here
this._hiddenPropertyForward(ajax, 'lastResponse', objectPath, propertyName);
// define a getter to trigger the request
Object.defineProperty(object, propertyName, {
enumerable: false,
configurable: true,
get: function() {
if (!ajax.loading) {
ajax.generateRequest();
}
},
set: function(value) {
if (ajax.loading) {
// if the property is explicitly set
// abort any request and set it directly
// the documentation tells about a discardRequest method but the real method is private
// fallback to the private method and use the documented one if present
var discardMethod = ajax.discardRequest || ajax._discardRequest;
discardMethod.call(ajax, ajax.lastRequest);
}
delete this[propertyName];
this[propertyName] = value;
}
});
// also give an indicator if the field is loading
var loadingProperty = propertyName + 'Loading';
this._hiddenPropertyForward(ajax, 'loading', objectPath, loadingProperty, true);
},
_createLinkPostMethod: function(object, path, propertyName, attributes) {
this._createLinkBodyMethod('post', object, propertyName, attributes);
},
_createLinkPutMethod: function(object, path, propertyName, attributes) {
this._createLinkBodyMethod('put', object, propertyName, attributes);
},
_composeMethodName: function(method, propertyName) {
return method + propertyName.substr(0, 1).toUpperCase() + propertyName.substr(1);
},
_createLinkBodyMethod: function(method, object, propertyName, attributes) {
var methodName = this._composeMethodName(method, propertyName);
var initiator = this;
Object.defineProperty(object, methodName, {
enumerable: false,
configurable: false,
value: function(data) {
var request = initiator._createSubInstance();
request.url = attributes.href;
request.method = method;
request.contentType = 'application/json';
request.body = JSON.stringify(data);
request.generateRequest();
return request;
}
});
},
_createLinkDeleteMethod: function(object, path, propertyName, attributes) {
var methodName = this._composeMethodName('delete', propertyName);
var initiator = this;
Object.defineProperty(object, methodName, {
enumerable: false,
configurable: false,
value: function(data) {
var request = initiator._createSubInstance();
request.url = attributes.href;
request.method = 'delete';
request.params = data;
request.generateRequest();
return request;
}
});
}
});
</script>
</dom-module>