Created
July 30, 2013 07:31
-
-
Save fredrikbonander/6110979 to your computer and use it in GitHub Desktop.
A simple service wrapping the $http api
This file contains 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
// Example usage of http-service | |
(function (window) { | |
"use strict"; | |
var angular = window.angular, | |
appModule = angular.module('app', ['httpService']); | |
appModule.controller('BaseCtrl', ['HttpService', function (HttpService) { | |
// Example GET, with query param, success and error callbacks | |
HttpService.get('/api/monkey', { | |
q: 'hello' // Query params | |
}, function (data) { | |
// success | |
}, function (data) { | |
// error | |
}); | |
// Example GET, without query param and just sucess callbacks | |
HttpService.get('/api/monkey', function (data) { | |
// success | |
}); | |
// Example POST, with post data and just sucess callbacks | |
HttpService.post('/api/monkey', { name: 'ram_' }, function (data) { | |
// success | |
}); | |
}]); | |
}(window)); |
This file contains 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
/** | |
* Created with PyCharm. | |
* User: broken | |
* Date: 6/12/13 | |
* Time: 1:50 PM | |
*/ | |
(function (window) { | |
"use strict"; | |
var angular = window.angular; | |
angular.module('httpService', []).factory('HttpService', ['$http', '$q', function ($http, $q) { | |
var globalDeferred = { | |
success: $q.defer(), | |
error: $q.defer() | |
}, | |
baseConfig = { | |
baseUrl: '' | |
}; | |
function query() { | |
function parseArguments(args) { | |
var httpData = null, | |
arg, | |
method = args.shift() || 'GET', | |
config = baseConfig, | |
url = args.shift(), | |
defaults = { | |
successCallback : globalDeferred.success, | |
errorCallback : globalDeferred.error, | |
canceler: $q.defer(), | |
httpOptions : {} | |
}; | |
while (args.length) { | |
arg = args.shift(); | |
if (angular.isFunction(arg)) { | |
if (!defaults.successCallback || defaults.successCallback === globalDeferred.success) { | |
defaults.successCallback = $q.defer(); | |
defaults.successCallback.promise.then(arg); | |
} else { | |
defaults.errorCallback = $q.defer(); | |
defaults.errorCallback.promise.then(arg); | |
} | |
} else if (angular.isObject(arg)) { | |
if (!httpData) { | |
httpData = arg; | |
} else { | |
config = angular.extend({}, baseConfig, arg); | |
} | |
} | |
} | |
defaults.httpOptions = {method: method, url: config.baseUrl + url, timeout: defaults.canceler.promise}; | |
if (defaults.httpOptions.method === 'GET' && httpData) { | |
defaults.httpOptions.params = httpData; | |
} else if (httpData) { | |
defaults.httpOptions.data = httpData; | |
} | |
return defaults; | |
} | |
var args = Array.prototype.slice.call(arguments), | |
options = parseArguments(args), | |
request; | |
request = $http(options.httpOptions).success(function () { | |
var args = Array.prototype.slice.call(arguments); | |
options.successCallback.resolve.apply(null, args); | |
}).error(function () { | |
var args = Array.prototype.slice.call(arguments); | |
options.errorCallback.resolve.apply(null, args); | |
}); | |
request.canceler = options.canceler; | |
request.abort = function () { | |
this.canceler.resolve(); | |
}; | |
return request; | |
} | |
function addGlobalCallback(callbackType, callback) { | |
var deferred = globalDeferred[callbackType]; | |
deferred.promise.then(callback); | |
} | |
function get() { | |
var args = Array.prototype.slice.call(arguments); | |
return query.apply(null, ['GET'].concat(args)); | |
} | |
function post() { | |
var args = Array.prototype.slice.call(arguments); | |
return query.apply(null, ['POST'].concat(args)); | |
} | |
function put() { | |
var args = Array.prototype.slice.call(arguments); | |
return query.apply(null, ['PUT'].concat(args)); | |
} | |
function doDelete() { | |
var args = Array.prototype.slice.call(arguments); | |
return query.apply(null, ['DELETE'].concat(args)); | |
} | |
return { | |
query: query, | |
get: get, | |
post: post, | |
put: put, | |
doDelete: doDelete, | |
config : baseConfig, | |
addGlobalCallback: addGlobalCallback | |
}; | |
}]); | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment