require 'app' .factory 'http', [ '$http' '$q' '$location' ( $http $q $location )-> prefix = 'admin/api' handle_data = (defer, r)-> if r.status is 200 and r.data.status is true defer.resolve r.data else if r.data.datas is 'Unauthorized' $location.path 'logout' defer.reject r.data http = (config)-> config.url = prefix + config.url d = $q.defer() $http config .then (r)-> handle_data d, r d.promise for method in [ 'get' 'head' 'post' 'put' 'delete' 'jsonp' ] do (method=method)-> http[method] = (url, args...)-> url = prefix + url d = $q.defer() $http[method] url, args... .then (r)-> handle_data d, r d.promise http ]