-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz-sails.js
58 lines (50 loc) · 1.81 KB
/
z-sails.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
/**
* Created by Sander Homan on 7/23/14.
*/
angular.module('z-sails', []);
(function(module){
module.provider('zSails', [function(){
var self = this;
self.useFileCheck = true;
self.useFallback = false;
this.$get = function(){
return {
useFileCheck: self.useFileCheck,
useFallback: self.useFallback
};
};
}]);
module.factory('$httpBackend', ['$httpXHRBackend', '$browser', 'zSails', '$window', function($httpXHRBackend, $browser, zsails, $window){
var $httpBackend = function(method, url, post, callback, headers, timeout, withCredentials, responseType){
url = url || $browser.url();
var lowercaseUrl;
if (zsails.useFileCheck)
lowercaseUrl = angular.lowercase(url);
var methodLowercase = angular.lowercase(method);
if ((methodLowercase !== 'get' && methodLowercase !== 'post' && methodLowercase !== 'put' && methodLowercase !== 'delete') ||
(zsails.useFileCheck && lowercaseUrl.length > 5 && (lowercaseUrl[lowercaseUrl.length-5] == '.' || lowercaseUrl[lowercaseUrl.length-4] == '.'))) //check if file. files will have a . at 3rd or 4th last character
{
return $httpXHRBackend(method, url, post, callback, headers, timeout, withCredentials, responseType);
}
else
{
// build request options
var options = {
method: methodLowercase,
url: url,
headers: headers,
data: angular.fromJson(post)
};
$window.io.socket._request(options, function(data, jwr){
if (zsails.useFallback && jwr.statusCode != 200)
{
$httpXHRBackend(method, url, post, callback, headers, timeout, withCredentials, responseType);
}
else
callback(jwr.statusCode, data, jwr.headers, "");
});
}
};
return $httpBackend;
}]);
})(angular.module('z-sails'));