-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgk-ajax.html
65 lines (56 loc) · 1.76 KB
/
gk-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
<element name="gk-ajax">
<template>
<div id="{{id}}" url="{{url}}" dataType="{{handleAs}}" data="{{data}}" type="{{method}}" headers="{{headers}}" contentType="{{contentType}}" withCredentials="{{withCredentials}}" xhrFields="{{xhrArgs}}" auto="{{auto}}"></div>
</template>
<script>
var utils = require('./js/utils'),
properties = ['url', 'data', 'headers', 'contentType', 'withCredentials', 'auto', {
'handleAs': 'dataType',
'method': 'type',
'xhrArgs': 'xhrFields'
}];
function getAjaxSettings($ele) {
var settings = {};
['url', 'dataType', 'type', 'contentType'].forEach(function(attr) {
settings[attr] = $ele.attr(attr);
});
['data', 'headers', 'xhrFields'].forEach(function(attr) {
var json = $ele.attr(attr);
json && (settings[attr] = JSON.parse(json));
});
settings.xhrFields = settings.xhrFields || {};
settings.xhrFields.withCredentials = !!$ele.attr('withCredentials');
settings.success = function(resp) {
$ele.gk().response = resp;
$ele.trigger('response', arguments);
};
settings.error = function() {
$ele.trigger('error', arguments);
};
settings.complete = function() {
$ele.trigger('complete', arguments);
};
return settings;
}
registerElement('gk-ajax', {
response: null,
init: function() {
var self = this;
utils.exposeProperties(properties, self,
function(attr) {
return self.$ele.attr(attr);
},
function(attr, val) {
self.$ele.attr(attr, val);
}
);
if (typeof self.auto !== 'undefined' && self.auto !== 'false') {
self.go();
}
},
go: function() {
$.ajax(getAjaxSettings(this.$ele));
}
});
</script>
</element>