Last active
August 29, 2015 14:14
-
-
Save weirongxu/49ea9228a7ba94116e9e to your computer and use it in GitHub Desktop.
ng-http-agent
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
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 | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment