@@ -43,16 +43,10 @@ export class CommonServiceBase {
4343
4444 this . length = null ;
4545
46- this . options = null ;
47-
4846 this . totalTimes = null ;
4947
5048 this . POLLING_TIMES = 3 ;
5149
52- this . _processSuccess = null ;
53-
54- this . _processFailed = null ;
55-
5650 this . isInTheSameDomain = null ;
5751
5852 this . withCredentials = false ;
@@ -105,7 +99,6 @@ export class CommonServiceBase {
10599 me . totalTimes = null ;
106100 }
107101 me . url = null ;
108- me . options = null ;
109102 me . _processSuccess = null ;
110103 me . _processFailed = null ;
111104 me . isInTheSameDomain = null ;
@@ -157,55 +150,66 @@ export class CommonServiceBase {
157150 options . url = SecurityManager . appendCredential ( options . url ) ;
158151
159152 me . calculatePollingTimes ( ) ;
160- me . _processSuccess = options . success ;
161- me . _processFailed = options . failure ;
162153 options . scope = me ;
163- options . success = me . getUrlCompleted ;
164- options . failure = me . getUrlFailed ;
165- me . options = options ;
166- me . _commit ( me . options ) ;
154+ var success = options . scope ? options . success . bind ( options . scope ) : options . success ;
155+ var failure = options . scope ? options . failure . bind ( options . scope ) : options . failure ;
156+ options . success = me . getUrlCompleted ( success , options ) ;
157+ options . failure = me . getUrlFailed ( failure , options ) ;
158+ me . _commit ( options ) ;
167159 }
168160
169161 /**
170162 * @function CommonServiceBase.prototype.getUrlCompleted
171163 * @description 请求成功后执行此方法。
172- * @param {Object } result - 服务器返回的结果对象。
164+ * @param {Object } cb - 成功回调函数。
165+ * @param {Object } options - 请求参数对象。
166+ * @private
173167 */
174- getUrlCompleted ( result ) {
175- let me = this ;
176- me . _processSuccess ( result ) ;
168+ getUrlCompleted ( cb , options ) {
169+ // @param {Object } result - 服务器返回的结果对象。
170+ return function ( result ) {
171+ cb && cb ( result , options ) ;
172+ }
177173 }
178174
179175 /**
180176 * @function CommonServiceBase.prototype.getUrlFailed
181177 * @description 请求失败后执行此方法。
182- * @param {Object } result - 服务器返回的结果对象。
178+
179+ * @param {Object } cb - 失败回调函数。
180+ * @param {Object } options - 请求参数对象。
181+ * @private
183182 */
184- getUrlFailed ( result ) {
185- let me = this ;
183+ getUrlFailed ( cb , options ) {
184+ const me = this ;
185+ // @param {Object } result - 服务器返回的结果对象。
186+ return function ( result ) {
186187 if ( me . totalTimes > 0 ) {
187- me . totalTimes -- ;
188- me . ajaxPolling ( ) ;
188+ me . totalTimes -- ;
189+ me . ajaxPolling ( options ) ;
189190 } else {
190- me . _processFailed ( result ) ;
191+ cb && cb ( result , options ) ;
191192 }
192- }
193+ }
194+ }
193195
194196 /**
195197 *
196198 * @function CommonServiceBase.prototype.ajaxPolling
197199 * @description 请求失败后,如果剩余请求失败次数不为 0,重新获取 URL 发送请求。
200+ * @param {Object } options - 请求参数对象。
201+ * @private
198202 */
199- ajaxPolling ( ) {
203+ ajaxPolling ( options ) {
200204 let me = this ,
201- url = me . options . url ,
205+ url = options . url ,
202206 re = / ^ h t t p : \/ \/ ( [ a - z ] { 9 } | ( \d + \. ) { 3 } \d + ) : \d { 0 , 4 } / ;
203207 me . index = parseInt ( Math . random ( ) * me . length ) ;
204208 me . url = me . urls [ me . index ] ;
205209 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 ) ;
210+ options . url = url ;
211+ options . isInTheSameDomain = Util . isInTheSameDomain ( url ) ;
212+ me . _commit ( options ) ;
209213 }
210214
211215 /**
@@ -249,24 +253,30 @@ export class CommonServiceBase {
249253 * @function CommonServiceBase.prototype.serviceProcessCompleted
250254 * @description 状态完成,执行此方法。
251255 * @param {Object } result - 服务器返回的结果对象。
256+ * @param {Object } options - 请求参数对象。
257+ * @private
252258 */
253- serviceProcessCompleted ( result ) {
259+ serviceProcessCompleted ( result , options ) {
254260 result = Util . transformResult ( result ) ;
255261 this . events . triggerEvent ( 'processCompleted' , {
256- result : result
262+ result : result ,
263+ options : options
257264 } ) ;
258265 }
259266
260267 /**
261268 * @function CommonServiceBase.prototype.serviceProcessFailed
262269 * @description 状态失败,执行此方法。
263270 * @param {Object } result - 服务器返回的结果对象。
271+ * @param {Object } options - 请求参数对象。对象
272+ * @private
264273 */
265- serviceProcessFailed ( result ) {
274+ serviceProcessFailed ( result , options ) {
266275 result = Util . transformResult ( result ) ;
267276 let error = result . error || result ;
268277 this . events . triggerEvent ( 'processFailed' , {
269- error : error
278+ error : error ,
279+ options : options
270280 } ) ;
271281 }
272282
@@ -376,4 +386,5 @@ export class CommonServiceBase {
376386 * @param {Object } serviceResult.object 发布应用程序事件的对象。
377387 * @param {Object } serviceResult.type 事件类型。
378388 * @param {Object } serviceResult.element 接受浏览器事件的 DOM 节点。
389+ * @param {Object } serviceResult.options 请求参数。
379390 */
0 commit comments