@@ -11,6 +11,7 @@ angular.module('ngSails').provider('$sails', function () {
11
11
12
12
this . url = undefined ;
13
13
this . interceptors = [ ] ;
14
+ this . responseHandler = undefined ;
14
15
15
16
this . $get = [ '$q' , '$timeout' , function ( $q , $timeout ) {
16
17
var socket = io . connect ( provider . url ) ,
@@ -19,23 +20,45 @@ angular.module('ngSails').provider('$sails', function () {
19
20
promise = deferred . promise ;
20
21
21
22
promise . success = function ( fn ) {
22
- promise . then ( fn ) ;
23
+ promise . then ( function ( response ) {
24
+ fn ( response . data , response . status , response . headers ) ;
25
+ } ) ;
23
26
return promise ;
24
27
} ;
25
28
26
29
promise . error = function ( fn ) {
27
- promise . then ( null , fn ) ;
30
+ promise . then ( null , function ( response ) {
31
+ fn ( response . data , response . status , response . headers ) ;
32
+ } ) ;
28
33
return promise ;
29
34
} ;
30
35
31
36
return deferred ;
32
37
} ,
33
- resolveOrReject = function ( deferred , data ) {
34
- // Make sure what is passed is an object that has a status and if that status is no 2xx, reject.
35
- if ( data && angular . isObject ( data ) && data . status && Math . floor ( data . status / 100 ) !== 2 ) {
36
- deferred . reject ( data ) ;
38
+ resolveOrReject = this . responseHandler || function ( deferred , response ) {
39
+ var jwr = response ;
40
+
41
+ // backward compatibility with older sails.io (no JWR)
42
+ if ( ! ( response instanceof Object && response . constructor . name === "JWR" ) ) {
43
+ jwr = {
44
+ body : response ,
45
+ headers : response . headers || { } ,
46
+ statusCode : response . statusCode || response . status
47
+ } ;
48
+ }
49
+
50
+ // angular $http returns the 'body' as 'data'.
51
+ jwr . data = jwr . body ;
52
+
53
+ // angular $http returns the 'statusCode' as 'status'.
54
+ jwr . status = jwr . statusCode ;
55
+
56
+ // TODO: map 'status'/'statusCode' to a 'statusText' to mimic angular $http
57
+
58
+ if ( jwr . error ) {
59
+ deferred . reject ( jwr ) ;
37
60
} else {
38
- deferred . resolve ( data ) ;
61
+ deferred . resolve ( jwr ) ;
39
62
}
40
63
} ,
41
64
angularify = function ( cb , data ) {
@@ -52,8 +75,8 @@ angular.module('ngSails').provider('$sails', function () {
52
75
data = null ;
53
76
}
54
77
deferred . promise . then ( cb ) ;
55
- socket [ 'legacy_' + methodName ] ( url , data , function ( result ) {
56
- resolveOrReject ( deferred , result ) ;
78
+ socket [ 'legacy_' + methodName ] ( url , data , function ( emulatedHTTPBody , jsonWebSocketResponse ) {
79
+ resolveOrReject ( deferred , jsonWebSocketResponse || emulatedHTTPBody ) ;
57
80
} ) ;
58
81
return deferred . promise ;
59
82
} ;
0 commit comments