@@ -43,16 +43,11 @@ export class CommonServiceBase {
43
43
44
44
this . length = null ;
45
45
46
- this . options = null ;
47
46
48
47
this . totalTimes = null ;
49
48
50
49
this . POLLING_TIMES = 3 ;
51
50
52
- this . _processSuccess = null ;
53
-
54
- this . _processFailed = null ;
55
-
56
51
this . isInTheSameDomain = null ;
57
52
58
53
this . withCredentials = false ;
@@ -105,7 +100,6 @@ export class CommonServiceBase {
105
100
me . totalTimes = null ;
106
101
}
107
102
me . url = null ;
108
- me . options = null ;
109
103
me . _processSuccess = null ;
110
104
me . _processFailed = null ;
111
105
me . isInTheSameDomain = null ;
@@ -157,55 +151,66 @@ export class CommonServiceBase {
157
151
options . url = SecurityManager . appendCredential ( options . url ) ;
158
152
159
153
me . calculatePollingTimes ( ) ;
160
- me . _processSuccess = options . success ;
161
- me . _processFailed = options . failure ;
162
154
options . scope = me ;
163
- options . success = me . getUrlCompleted ;
164
- options . failure = me . getUrlFailed ;
165
- me . options = options ;
166
- me . _commit ( me . options ) ;
155
+ var success = options . scope ? options . success . bind ( options . scope ) : options . success ;
156
+ var failure = options . scope ? options . failure . bind ( options . scope ) : options . failure ;
157
+ options . success = me . getUrlCompleted ( success , options ) ;
158
+ options . failure = me . getUrlFailed ( failure , options ) ;
159
+ me . _commit ( options ) ;
167
160
}
168
161
169
162
/**
170
163
* @function CommonServiceBase.prototype.getUrlCompleted
171
164
* @description 请求成功后执行此方法。
172
- * @param {Object } result - 服务器返回的结果对象。
165
+ * @param {Object } cb - 成功回调函数。
166
+ * @param {Object } options - 请求参数对象。
167
+ * @private
173
168
*/
174
- getUrlCompleted ( result ) {
175
- let me = this ;
176
- me . _processSuccess ( result ) ;
169
+ getUrlCompleted ( cb , options ) {
170
+ // @param {Object } result - 服务器返回的结果对象。
171
+ return function ( result ) {
172
+ cb && cb ( result , options ) ;
173
+ }
177
174
}
178
175
179
176
/**
180
177
* @function CommonServiceBase.prototype.getUrlFailed
181
178
* @description 请求失败后执行此方法。
182
- * @param {Object } result - 服务器返回的结果对象。
179
+
180
+ * @param {Object } cb - 失败回调函数。
181
+ * @param {Object } options - 请求参数对象。
182
+ * @private
183
183
*/
184
- getUrlFailed ( result ) {
185
- let me = this ;
184
+ getUrlFailed ( cb , options ) {
185
+ const me = this ;
186
+ // @param {Object } result - 服务器返回的结果对象。
187
+ return function ( result ) {
186
188
if ( me . totalTimes > 0 ) {
187
- me . totalTimes -- ;
188
- me . ajaxPolling ( ) ;
189
+ me . totalTimes -- ;
190
+ me . ajaxPolling ( options ) ;
189
191
} else {
190
- me . _processFailed ( result ) ;
192
+ cb && cb ( result , options ) ;
191
193
}
192
- }
194
+ }
195
+ }
193
196
194
197
/**
195
198
*
196
199
* @function CommonServiceBase.prototype.ajaxPolling
197
200
* @description 请求失败后,如果剩余请求失败次数不为 0,重新获取 URL 发送请求。
201
+ * @param {Object } options - 请求参数对象。
202
+ * @private
198
203
*/
199
- ajaxPolling ( ) {
204
+ ajaxPolling ( options ) {
200
205
let me = this ,
201
- url = me . options . url ,
206
+ url = options . url ,
202
207
re = / ^ h t t p : \/ \/ ( [ a - z ] { 9 } | ( \d + \. ) { 3 } \d + ) : \d { 0 , 4 } / ;
203
208
me . index = parseInt ( Math . random ( ) * me . length ) ;
204
209
me . url = me . urls [ me . index ] ;
205
210
url = url . replace ( re , re . exec ( me . url ) [ 0 ] ) ;
206
- me . options . url = url ;
207
- me . options . isInTheSameDomain = Util . isInTheSameDomain ( url ) ;
208
- me . _commit ( me . options ) ;
211
+ options . url = url ;
212
+ options . isInTheSameDomain = Util . isInTheSameDomain ( url ) ;
213
+ me . _commit ( options ) ;
209
214
}
210
215
211
216
/**
@@ -249,24 +254,30 @@ export class CommonServiceBase {
249
254
* @function CommonServiceBase.prototype.serviceProcessCompleted
250
255
* @description 状态完成,执行此方法。
251
256
* @param {Object } result - 服务器返回的结果对象。
257
+ * @param {Object } options - 请求参数对象。
258
+ * @private
252
259
*/
253
- serviceProcessCompleted ( result ) {
260
+ serviceProcessCompleted ( result , options ) {
254
261
result = Util . transformResult ( result ) ;
255
262
this . events . triggerEvent ( 'processCompleted' , {
256
- result : result
263
+ result : result ,
264
+ options : options
257
265
} ) ;
258
266
}
259
267
260
268
/**
261
269
* @function CommonServiceBase.prototype.serviceProcessFailed
262
270
* @description 状态失败,执行此方法。
263
271
* @param {Object } result - 服务器返回的结果对象。
272
+ * @param {Object } options - 请求参数对象。对象
273
+ * @private
264
274
*/
265
- serviceProcessFailed ( result ) {
275
+ serviceProcessFailed ( result , options ) {
266
276
result = Util . transformResult ( result ) ;
267
277
let error = result . error || result ;
268
278
this . events . triggerEvent ( 'processFailed' , {
269
- error : error
279
+ error : error ,
280
+ options : options
270
281
} ) ;
271
282
}
272
283
0 commit comments