@@ -43,16 +43,11 @@ export class CommonServiceBase {
4343
4444 this . length = null ;
4545
46- this . options = null ;
4746
4847 this . totalTimes = null ;
4948
5049 this . POLLING_TIMES = 3 ;
5150
52- this . _processSuccess = null ;
53-
54- this . _processFailed = null ;
55-
5651 this . isInTheSameDomain = null ;
5752
5853 this . withCredentials = false ;
@@ -105,7 +100,6 @@ export class CommonServiceBase {
105100 me . totalTimes = null ;
106101 }
107102 me . url = null ;
108- me . options = null ;
109103 me . _processSuccess = null ;
110104 me . _processFailed = null ;
111105 me . isInTheSameDomain = null ;
@@ -157,55 +151,66 @@ export class CommonServiceBase {
157151 options . url = SecurityManager . appendCredential ( options . url ) ;
158152
159153 me . calculatePollingTimes ( ) ;
160- me . _processSuccess = options . success ;
161- me . _processFailed = options . failure ;
162154 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 ) ;
167160 }
168161
169162 /**
170163 * @function CommonServiceBase.prototype.getUrlCompleted
171164 * @description 请求成功后执行此方法。
172- * @param {Object } result - 服务器返回的结果对象。
165+ * @param {Object } cb - 成功回调函数。
166+ * @param {Object } options - 请求参数对象。
167+ * @private
173168 */
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+ }
177174 }
178175
179176 /**
180177 * @function CommonServiceBase.prototype.getUrlFailed
181178 * @description 请求失败后执行此方法。
182- * @param {Object } result - 服务器返回的结果对象。
179+
180+ * @param {Object } cb - 失败回调函数。
181+ * @param {Object } options - 请求参数对象。
182+ * @private
183183 */
184- getUrlFailed ( result ) {
185- let me = this ;
184+ getUrlFailed ( cb , options ) {
185+ const me = this ;
186+ // @param {Object } result - 服务器返回的结果对象。
187+ return function ( result ) {
186188 if ( me . totalTimes > 0 ) {
187- me . totalTimes -- ;
188- me . ajaxPolling ( ) ;
189+ me . totalTimes -- ;
190+ me . ajaxPolling ( options ) ;
189191 } else {
190- me . _processFailed ( result ) ;
192+ cb && cb ( result , options ) ;
191193 }
192- }
194+ }
195+ }
193196
194197 /**
195198 *
196199 * @function CommonServiceBase.prototype.ajaxPolling
197200 * @description 请求失败后,如果剩余请求失败次数不为 0,重新获取 URL 发送请求。
201+ * @param {Object } options - 请求参数对象。
202+ * @private
198203 */
199- ajaxPolling ( ) {
204+ ajaxPolling ( options ) {
200205 let me = this ,
201- url = me . options . url ,
206+ url = options . url ,
202207 re = / ^ h t t p : \/ \/ ( [ a - z ] { 9 } | ( \d + \. ) { 3 } \d + ) : \d { 0 , 4 } / ;
203208 me . index = parseInt ( Math . random ( ) * me . length ) ;
204209 me . url = me . urls [ me . index ] ;
205210 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 ) ;
209214 }
210215
211216 /**
@@ -249,24 +254,30 @@ export class CommonServiceBase {
249254 * @function CommonServiceBase.prototype.serviceProcessCompleted
250255 * @description 状态完成,执行此方法。
251256 * @param {Object } result - 服务器返回的结果对象。
257+ * @param {Object } options - 请求参数对象。
258+ * @private
252259 */
253- serviceProcessCompleted ( result ) {
260+ serviceProcessCompleted ( result , options ) {
254261 result = Util . transformResult ( result ) ;
255262 this . events . triggerEvent ( 'processCompleted' , {
256- result : result
263+ result : result ,
264+ options : options
257265 } ) ;
258266 }
259267
260268 /**
261269 * @function CommonServiceBase.prototype.serviceProcessFailed
262270 * @description 状态失败,执行此方法。
263271 * @param {Object } result - 服务器返回的结果对象。
272+ * @param {Object } options - 请求参数对象。对象
273+ * @private
264274 */
265- serviceProcessFailed ( result ) {
275+ serviceProcessFailed ( result , options ) {
266276 result = Util . transformResult ( result ) ;
267277 let error = result . error || result ;
268278 this . events . triggerEvent ( 'processFailed' , {
269- error : error
279+ error : error ,
280+ options : options
270281 } ) ;
271282 }
272283
0 commit comments