-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhe-ajax.js
47 lines (38 loc) · 1.39 KB
/
he-ajax.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
angular.module('helium', []).
directive('heAjax', ['$http', '$parse', '$compile',
function heAjaxDir($http, $parse, $compile) {
return{
restrict: 'A',
compile: function heAjaxCompile(tElement, tAttrs) {
tElement.attr('ng-cloak','');
var heAjax = eval( '(' + tAttrs.heAjax + ')');
var heAjaxScope = heAjax.scope;
var heAjaxUrl = heAjax.url;
var heAjaxKey = heAjax.key;
//we shouldn't do anything if url is not present
if(!angular.isDefined(heAjaxUrl)){
throw '"url" is not defined';
}
//Adding ng native bingings
$compile.$$addBindingClass(tElement);
return function heAjaxLink(scope, element, attr) {
//Adding ng native bingings
$compile.$$addBindingInfo(element, heAjaxUrl);
//response body, should check response status?
$http.get(heAjaxUrl).then(function(response) {
element.removeAttr('ng-cloak');
//priority=> scope: key: DOM
if(angular.isDefined(heAjaxScope)){
scope[heAjaxScope] = angular.fromJson(response.data);
}
else if(angular.isDefined(heAjaxKey)){
element.html(angular.fromJson(response.data)[heAjaxKey]);
}
else{
element.html(angular.fromJson(response.data));
}
});
};
}
}
}]);