|
21 | 21 | /////////////
|
22 | 22 |
|
23 | 23 | function getPhones () {
|
24 |
| - var d = $q.defer(); |
25 |
| - $http.get('api/phones') |
26 |
| - .success(success) |
27 |
| - .error(fail); |
28 |
| - return d.promise; |
29 |
| - |
30 |
| - function success (response, status) { |
31 |
| - if (status === 200 && response.code === 0) { |
32 |
| - d.resolve(response.result.phones); |
| 24 | + return $http.get('api/phones') |
| 25 | + .then(success) |
| 26 | + .catch(fail); |
| 27 | + |
| 28 | + function success (response) { |
| 29 | + var data = response.data; |
| 30 | + if (response.status === 200 && data.code === 0) { |
| 31 | + return data.result.phones; |
33 | 32 | } else {
|
34 |
| - d.reject(response.message); |
| 33 | + return $q.reject(data.message); |
35 | 34 | }
|
36 | 35 | }
|
37 | 36 |
|
38 | 37 | function fail () {
|
39 |
| - d.reject('$SERVER'); |
| 38 | + return $q.reject('$SERVER'); |
40 | 39 | }
|
41 | 40 | }
|
42 | 41 |
|
43 | 42 | function getPhoneDetail (id) {
|
44 |
| - var d = $q.defer(); |
45 |
| - $http.get('api/phones/' + id) |
46 |
| - .success(success) |
47 |
| - .error(fail); |
48 |
| - return d.promise; |
49 |
| - |
50 |
| - function success (response, status) { |
51 |
| - if (status === 200 && response.code === 0) { |
52 |
| - d.resolve(response.result.phone); |
| 43 | + return $http.get('api/phones/' + id) |
| 44 | + .then(success) |
| 45 | + .catch(fail); |
| 46 | + |
| 47 | + function success (response) { |
| 48 | + var data = response.data; |
| 49 | + if (response.status === 200 && data.code === 0) { |
| 50 | + return data.result.phone; |
53 | 51 | } else {
|
54 |
| - d.reject(response.message); |
| 52 | + return $q.reject(data.message); |
55 | 53 | }
|
56 | 54 | }
|
57 | 55 |
|
58 | 56 | function fail () {
|
59 |
| - d.reject('$SERVER'); |
| 57 | + return $q.reject('$SERVER'); |
60 | 58 | }
|
61 | 59 | }
|
62 | 60 |
|
63 | 61 | function addNewPhone (phone) {
|
64 |
| - var d = $q.defer(); |
65 | 62 | var req = {
|
66 | 63 | 'phone': phone
|
67 | 64 | };
|
68 |
| - $http.post('api/phones', req) |
69 |
| - .success(success) |
70 |
| - .error(fail); |
71 |
| - return d.promise; |
72 |
| - |
73 |
| - function success (response, status) { |
74 |
| - if (status === 200 && response.code === 0) { |
75 |
| - d.resolve(response.result.phone); |
| 65 | + return $http.post('api/phones', req) |
| 66 | + .then(success) |
| 67 | + .catch(fail); |
| 68 | + |
| 69 | + function success (response) { |
| 70 | + var data = response.data; |
| 71 | + if (response.status === 200 && data.code === 0) { |
| 72 | + return data.result.phone; |
76 | 73 | } else {
|
77 |
| - d.reject(response.message); |
| 74 | + return $q.reject(data.message); |
78 | 75 | }
|
79 | 76 | }
|
80 | 77 |
|
81 | 78 | function fail () {
|
82 |
| - d.reject('$SERVER'); |
| 79 | + return $q.reject('$SERVER'); |
83 | 80 | }
|
84 | 81 | }
|
85 | 82 |
|
86 | 83 | function updatePhone (id, phone) {
|
87 |
| - var d = $q.defer(); |
88 | 84 | var req = {
|
89 | 85 | 'phone': phone
|
90 | 86 | };
|
91 |
| - $http.put('api/phones/' + id, req) |
92 |
| - .success(success) |
93 |
| - .error(fail); |
94 |
| - return d.promise; |
95 |
| - |
96 |
| - function success (response, status) { |
97 |
| - if (status === 200 && response.code === 0) { |
98 |
| - d.resolve(response.result.phone); |
| 87 | + return $http.put('api/phones/' + id, req) |
| 88 | + .then(success) |
| 89 | + .catch(fail); |
| 90 | + |
| 91 | + function success (response) { |
| 92 | + var data = response.data; |
| 93 | + if (response.status === 200 && data.code === 0) { |
| 94 | + return data.result.phone; |
99 | 95 | } else {
|
100 |
| - d.reject(response.message); |
| 96 | + return $q.reject(data.message); |
101 | 97 | }
|
102 | 98 | }
|
103 | 99 |
|
104 | 100 | function fail () {
|
105 |
| - d.reject('$SERVER'); |
| 101 | + return $q.reject('$SERVER'); |
106 | 102 | }
|
107 | 103 | }
|
108 | 104 |
|
109 | 105 | function removePhone (id) {
|
110 |
| - var d = $q.defer(); |
111 |
| - $http.delete('api/phones/' + id) |
112 |
| - .success(success) |
113 |
| - .error(fail); |
114 |
| - return d.promise; |
115 |
| - |
116 |
| - function success (response, status) { |
117 |
| - if (status === 200 && response.code === 0) { |
118 |
| - d.resolve(response.result.phone); |
| 106 | + return $http.delete('api/phones/' + id) |
| 107 | + .then(success) |
| 108 | + .catch(fail); |
| 109 | + |
| 110 | + function success (response) { |
| 111 | + var data = response.data; |
| 112 | + if (response.status === 200 && data.code === 0) { |
| 113 | + return data.result.phone; |
119 | 114 | } else {
|
120 |
| - d.reject(response.message); |
| 115 | + return $q.reject(data.message); |
121 | 116 | }
|
122 | 117 | }
|
123 | 118 |
|
124 | 119 | function fail () {
|
125 |
| - d.reject('$SERVER'); |
| 120 | + return $q.reject('$SERVER'); |
126 | 121 | }
|
127 | 122 | }
|
128 | 123 |
|
|
0 commit comments