Skip to content

Commit d451020

Browse files
[fix]common去掉me.options review by qiw
1 parent 566c237 commit d451020

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

src/common/iServer/CommonServiceBase.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^http:\/\/([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

src/common/iServer/ImageCollectionService.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { CommonServiceBase } from './CommonServiceBase';
2222
export default class ImageCollectionService extends CommonServiceBase {
2323
constructor(url, options) {
2424
super(url, options);
25-
this._serviceOptions = options || {};
25+
this.options = options || {};
2626
if (options) {
2727
Util.extend(this, options);
2828
}
@@ -47,7 +47,7 @@ export default class ImageCollectionService extends CommonServiceBase {
4747
getLegend(queryParams, callback) {
4848
var me = this;
4949
var pathParams = {
50-
collectionId: me._serviceOptions.collectionId
50+
collectionId: me.options.collectionId
5151
};
5252
var path = Util.convertPath('/collections/{collectionId}/legend', pathParams);
5353
var url = Util.urlPathAppend(me.url, path);
@@ -61,7 +61,7 @@ export default class ImageCollectionService extends CommonServiceBase {
6161
getStatistics(callback) {
6262
var me = this;
6363
var pathParams = {
64-
collectionId: me._serviceOptions.collectionId
64+
collectionId: me.options.collectionId
6565
};
6666
var path = Util.convertPath('/collections/{collectionId}/statistics', pathParams);
6767
var url = Util.urlPathAppend(me.url, path);
@@ -76,7 +76,7 @@ export default class ImageCollectionService extends CommonServiceBase {
7676
getTileInfo(callback) {
7777
var me = this;
7878
var pathParams = {
79-
collectionId: me._serviceOptions.collectionId
79+
collectionId: me.options.collectionId
8080
};
8181
var path = Util.convertPath('/collections/{collectionId}/tileInfo', pathParams);
8282
var url = Util.urlPathAppend(me.url, path);
@@ -91,7 +91,7 @@ export default class ImageCollectionService extends CommonServiceBase {
9191
deleteItemByID(featureId, callback) {
9292
var me = this;
9393
var pathParams = {
94-
collectionId: me._serviceOptions.collectionId,
94+
collectionId: me.options.collectionId,
9595
featureId: featureId
9696
};
9797
var path = Util.convertPath('/collections/{collectionId}/items/{featureId}', pathParams);
@@ -107,7 +107,7 @@ export default class ImageCollectionService extends CommonServiceBase {
107107
getItemByID(featureId, callback) {
108108
var me = this;
109109
var pathParams = {
110-
collectionId: me._serviceOptions.collectionId,
110+
collectionId: me.options.collectionId,
111111
featureId: featureId
112112
};
113113
var path = Util.convertPath('/collections/{collectionId}/items/{featureId}', pathParams);
@@ -137,16 +137,16 @@ export default class ImageCollectionService extends CommonServiceBase {
137137
url,
138138
params,
139139
scope: this,
140-
success(result) {
140+
success(result, options) {
141141
result.eventId = eventId;
142-
this.serviceProcessCompleted(result);
142+
this.serviceProcessCompleted(result, options);
143143
},
144-
failure(result) {
144+
failure(result, options) {
145145
if (result.error) {
146146
result.error.eventId = eventId;
147147
}
148148
result.eventId = eventId;
149-
this.serviceProcessFailed(result);
149+
this.serviceProcessFailed(result, options);
150150
}
151151
});
152152
}

test/common/iServer/ImageCollectionServiceSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ describe('ImageCollectionService', () => {
579579

580580
it('should call search options null', function (done) {
581581
service = new ImageCollectionService(null);
582-
expect(Object.keys(service._serviceOptions).length).toBe(0);
582+
expect(Object.keys(service.options).length).toBe(0);
583583
done();
584584
});
585585
});

0 commit comments

Comments
 (0)