Skip to content

Instantly share code, notes, and snippets.

@weirongxu
Last active August 29, 2015 14:14
Show Gist options
  • Save weirongxu/49ea9228a7ba94116e9e to your computer and use it in GitHub Desktop.
Save weirongxu/49ea9228a7ba94116e9e to your computer and use it in GitHub Desktop.
ng-http-agent
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