@@ -7,6 +7,7 @@ import { SecurityManager } from '../security/SecurityManager';
77import { Util } from '../commontypes/Util' ;
88import { JSONFormat } from '../format/JSON' ;
99import { DataFormat } from '../REST' ;
10+ import { FunctionExt } from '../commontypes/BaseTypes' ;
1011
1112/**
1213 * @class CommonServiceBase
@@ -25,7 +26,7 @@ export class CommonServiceBase {
2526 constructor ( url , options ) {
2627 let me = this ;
2728
28- this . EVENT_TYPES = [ ] ;
29+ this . EVENT_TYPES = [ 'processCompleted' , 'processFailed' ] ;
2930
3031 this . events = null ;
3132
@@ -219,28 +220,63 @@ export class CommonServiceBase {
219220 }
220221
221222 /**
222- * @function CommonServiceBase.prototype.serviceProcessCompleted
223- * @description 状态完成,执行此方法 。
223+ * @function CommonServiceBase.prototype.transformResult
224+ * @description 状态完成时转换结果 。
224225 * @param {Object } result - 服务器返回的结果对象。
226+ * @param {Object } options - 请求参数。
227+ * @return {Object } 转换结果。
225228 * @private
226229 */
227- serviceProcessCompleted ( result , options ) {
230+ transformResult ( result , options ) {
228231 result = Util . transformResult ( result ) ;
229232 return { result, options } ;
230233 }
231234
232235 /**
233- * @function CommonServiceBase.prototype.serviceProcessFailed
234- * @description 状态失败,执行此方法 。
236+ * @function CommonServiceBase.prototype.transformErrorResult
237+ * @description 状态失败时转换结果 。
235238 * @param {Object } result - 服务器返回的结果对象。
239+ * @param {Object } options - 请求参数。
240+ * @return {Object } 转换结果。
236241 * @private
237242 */
238- serviceProcessFailed ( result , options ) {
243+ transformErrorResult ( result , options ) {
239244 result = Util . transformResult ( result ) ;
240245 let error = result . error || result ;
241246 return { error, options } ;
242247 }
243248
249+ /**
250+ * @function CommonServiceBase.prototype.serviceProcessCompleted
251+ * @description 状态完成,执行此方法。
252+ * @param {Object } result - 服务器返回的结果对象。
253+ * @param {Object } options - 请求参数对象。
254+ * @private
255+ */
256+ serviceProcessCompleted ( result , options ) {
257+ result = Util . transformResult ( result ) ;
258+ this . events . triggerEvent ( 'processCompleted' , {
259+ result : result ,
260+ options : options
261+ } ) ;
262+ }
263+
264+ /**
265+ * @function CommonServiceBase.prototype.serviceProcessFailed
266+ * @description 状态失败,执行此方法。
267+ * @param {Object } result - 服务器返回的结果对象。
268+ * @param {Object } options - 请求参数对象。对象
269+ * @private
270+ */
271+ serviceProcessFailed ( result , options ) {
272+ result = Util . transformResult ( result ) ;
273+ let error = result . error || result ;
274+ this . events . triggerEvent ( 'processFailed' , {
275+ error : error ,
276+ options : options
277+ } ) ;
278+ }
279+
244280 _returnContent ( options ) {
245281 if ( options . scope . format === DataFormat . FGB ) {
246282 return false ;
@@ -324,14 +360,25 @@ export class CommonServiceBase {
324360 object : this
325361 } ;
326362 if ( requestResult . error ) {
327- response = { ...response , ...this . serviceProcessFailed ( requestResult , options ) } ;
363+ // 兼容服务在构造函数中使用 eventListeners 的老用法
364+ if ( options . failure === this . serviceProcessFailed ) {
365+ var failure = options . scope ? FunctionExt . bind ( options . failure , options . scope ) : options . failure ;
366+ failure ( requestResult , options ) ;
367+ } else {
368+ response = { ...response , ...this . transformErrorResult ( requestResult , options ) } ;
328369 response . type = 'processFailed' ;
329370 options . failure && options . failure ( response ) ;
371+ }
330372 } else {
373+ if ( options . success === this . serviceProcessCompleted ) {
374+ var success = options . scope ? FunctionExt . bind ( options . success , options . scope ) : options . success ;
375+ success ( requestResult , options ) ;
376+ } else {
331377 requestResult . succeed = requestResult . succeed == undefined ? true : requestResult . succeed ;
332- response = { ...response , ...this . serviceProcessCompleted ( requestResult , options ) } ;
378+ response = { ...response , ...this . transformResult ( requestResult , options ) } ;
333379 response . type = 'processCompleted' ;
334380 options . success && options . success ( response ) ;
381+ }
335382 }
336383 return response ;
337384 } ) ;
0 commit comments