Last active
August 29, 2015 14:00
-
-
Save carmichaelize/11183176 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('myApp.services', []) | |
.service('$_http', function($http, $q, $compile) { | |
//Serialize Parameters | |
var $param = function(obj){ | |
var query = '', | |
name, value, fullSubName, subName, subValue, innerObj, i; | |
for(name in obj){ | |
value = obj[name]; | |
if(value instanceof Array){ | |
for(i=0; i<value.length; ++i){ | |
subValue = value[i]; | |
fullSubName = name + '[' + i + ']'; | |
innerObj = {}; | |
innerObj[fullSubName] = subValue; | |
query += param(innerObj) + '&'; | |
} | |
} else if(value instanceof Object){ | |
for(subName in value){ | |
subValue = value[subName]; | |
fullSubName = name + '[' + subName + ']'; | |
innerObj = {}; | |
innerObj[fullSubName] = subValue; | |
query += param(innerObj) + '&'; | |
} | |
} else if(value !== undefined && value !== null){ | |
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; | |
} | |
} | |
return query.length ? query.substr(0, query.length - 1) : query; | |
}; | |
return { | |
load: function(template, target, $scope){ | |
var deferred = $q.defer(); | |
$http({method: 'GET', url: template}) | |
.success(function(data, status, headers, config) { | |
target.html($compile(data)($scope)); | |
deferred.resolve(); | |
}) | |
.error(function(data, status, headers, config) { | |
deferred.reject(); | |
}); | |
return deferred.promise; | |
}, | |
get : function(endpoint, data){ | |
window.isLoading(); | |
var deferred = $q.defer(), | |
data = typeof data == 'object' ? data : {}, | |
id = data.id ? data.id : false; | |
delete data.id; | |
var data = $param(data) ? "?"+$param(data) : "", | |
url = id ? "/api/"+endpoint+"/"+id+""+data : "/api/"+endpoint+""+data; | |
$http({method: 'GET', url: url}) | |
.success(function(data, status, headers, config) { | |
window.isLoaded(); | |
deferred.resolve(data); | |
}) | |
.error(function(data, status, headers, config) { | |
window.isError(data); | |
deferred.reject(data); | |
}); | |
return deferred.promise; | |
}, | |
post : function(endpoint, data){ | |
var deferred = $q.defer(), | |
method = data.id ? "PUT" : "POST", | |
url = data.id ? "/api/"+endpoint+"/"+data.id : "/api/"+endpoint; | |
$http({method: method, url: url, data: data }) | |
.success(function(data, status, headers, config) { | |
deferred.resolve(data); | |
}) | |
.error(function(data, status, headers, config) { | |
deferred.reject(data); | |
}); | |
return deferred.promise; | |
}, | |
delete : function(endpoint, id){ | |
var deferred = $q.defer(), | |
validate = window.confirm('Are you sure you wish to delete this item?'); | |
if( validate ){ | |
$http({method: 'DELETE', url: "/api/"+endpoint+"/"+id }) | |
.success(function(data, status, headers, config) { | |
deferred.resolve(data); | |
}) | |
.error(function(data, status, headers, config) { | |
deferred.reject(data); | |
}); | |
return deferred.promise; | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment