Skip to content

Commit 2c81dd8

Browse files
committed
弃用方法增加 warn 及变量名优化
1 parent 2d4eba3 commit 2c81dd8

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/utils.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
'use strict';
77

8-
module.exports = function(AV) {
8+
const _ = require('underscore');
99

10-
const _ = AV._;
10+
module.exports = function(AV) {
1111

1212
// 挂载一些配置
13-
let avConfig = AV._config;
13+
let AVConfig = AV._config;
1414

15-
_.extend(avConfig, {
15+
_.extend(AVConfig, {
1616

1717
// 服务器请求的节点 host
1818
apiHost : {
@@ -34,7 +34,7 @@ module.exports = function(AV) {
3434

3535
// Check whether we are running in Node.js.
3636
if (typeof(process) !== 'undefined' && process.versions && process.versions.node) {
37-
avConfig.isNode = true;
37+
AVConfig.isNode = true;
3838
}
3939

4040
// Helpers
@@ -123,15 +123,15 @@ module.exports = function(AV) {
123123
case 1:
124124
const options = args[0];
125125
if (typeof options === 'object') {
126-
if (!avConfig.isNode && options.masterKey) {
126+
if (!AVConfig.isNode && options.masterKey) {
127127
throw new Error('AV.init(): Master Key is only used in Node.js.');
128128
}
129129
initialize(options.appId, options.appKey, options.masterKey);
130130

131131
// 服务器地区选项,默认为中国大陆
132132
switch (options.region) {
133133
case 'us':
134-
avConfig.region = 'us';
134+
AVConfig.region = 'us';
135135
break;
136136
}
137137
} else {
@@ -141,7 +141,8 @@ module.exports = function(AV) {
141141
// 兼容旧版本的初始化方法
142142
case 2:
143143
case 3:
144-
if (!avConfig.isNode && args.length === 3) {
144+
console.warn('Please use AV.init() replace AV.initialize() .');
145+
if (!AVConfig.isNode && args.length === 3) {
145146
throw new Error('AV.init(): Master Key is only used in Node.js.');
146147
}
147148
initialize(...args);
@@ -150,7 +151,7 @@ module.exports = function(AV) {
150151
};
151152

152153
// If we're running in node.js, allow using the master key.
153-
if (avConfig.isNode) {
154+
if (AVConfig.isNode) {
154155
AV.Cloud = AV.Cloud || {};
155156
/**
156157
* Switches the LeanCloud SDK to using the Master key. The Master key grants
@@ -182,14 +183,22 @@ module.exports = function(AV) {
182183
AV.applicationProduction = AV._isNullOrUndefined(production) ? 1: production;
183184
};
184185

186+
/**
187+
* @deprecated Please use AV.init(), you can set the region of server .
188+
**/
185189
// TODO: 后续不再暴露此接口
186190
AV.useAVCloudCN = function(){
187-
avConfig.region = 'cn';
191+
AVConfig.region = 'cn';
192+
console.warn('Do not use AV.useAVCloudCN. Please use AV.init(), you can set the region of server.');
188193
};
189194

195+
/**
196+
* @deprecated Please use AV.init(), you can set the region of server .
197+
**/
190198
// TODO: 后续不再暴露此接口
191199
AV.useAVCloudUS = function(){
192-
avConfig.region = 'us';
200+
AVConfig.region = 'us';
201+
console.warn('Do not use AV.useAVCloudUS. Please use AV.init(), you can set the region of server.');
193202
};
194203

195204
/**
@@ -205,7 +214,7 @@ module.exports = function(AV) {
205214
if (!path) {
206215
path = "";
207216
}
208-
if (!AV._.isString(path)) {
217+
if (!_.isString(path)) {
209218
throw "Tried to get a localStorage path that wasn't a String.";
210219
}
211220
if (path[0] === "/") {
@@ -329,7 +338,7 @@ module.exports = function(AV) {
329338
}
330339

331340
// 兼容 AV.serverURL 旧方式设置 API Host,后续去掉
332-
let apiUrl = AV.serverURL || avConfig.apiHost[avConfig.region];
341+
let apiUrl = AV.serverURL || AVConfig.apiHost[AVConfig.region];
333342
if (apiUrl.charAt(apiUrl.length - 1) !== "/") {
334343
apiUrl += "/";
335344
}
@@ -345,7 +354,7 @@ module.exports = function(AV) {
345354
apiUrl += '?new=true';
346355
}
347356

348-
dataObject = AV._.clone(dataObject || {});
357+
dataObject = _.clone(dataObject || {});
349358
if (method !== "POST") {
350359
dataObject._method = method;
351360
method = "POST";
@@ -397,7 +406,7 @@ module.exports = function(AV) {
397406
if (!(object && object[prop])) {
398407
return null;
399408
}
400-
return AV._.isFunction(object[prop]) ? object[prop]() : object[prop];
409+
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
401410
};
402411

403412
/**
@@ -570,7 +579,7 @@ module.exports = function(AV) {
570579
}
571580
};
572581

573-
AV._arrayEach = AV._.each;
582+
AV._arrayEach = _.each;
574583

575584
/**
576585
* Does a deep traversal of every item in object, calling func on every one.
@@ -583,7 +592,7 @@ module.exports = function(AV) {
583592
AV._traverse = function(object, func, seen) {
584593
if (object instanceof AV.Object) {
585594
seen = seen || [];
586-
if (AV._.indexOf(seen, object) >= 0) {
595+
if (_.indexOf(seen, object) >= 0) {
587596
// We've already visited this object in this call.
588597
return;
589598
}
@@ -596,16 +605,16 @@ module.exports = function(AV) {
596605
// object's parent infinitely, so we catch this case.
597606
return func(object);
598607
}
599-
if (AV._.isArray(object)) {
600-
AV._.each(object, function(child, index) {
608+
if (_.isArray(object)) {
609+
_.each(object, function(child, index) {
601610
var newChild = AV._traverse(child, func, seen);
602611
if (newChild) {
603612
object[index] = newChild;
604613
}
605614
});
606615
return func(object);
607616
}
608-
if (AV._.isObject(object)) {
617+
if (_.isObject(object)) {
609618
AV._each(object, function(child, key) {
610619
var newChild = AV._traverse(child, func, seen);
611620
if (newChild) {
@@ -634,6 +643,6 @@ module.exports = function(AV) {
634643

635644
// Helper function to check null or undefined.
636645
AV._isNullOrUndefined = function(x) {
637-
return AV._.isNull(x) || AV._.isUndefined(x);
646+
return _.isNull(x) || _.isUndefined(x);
638647
};
639648
};

0 commit comments

Comments
 (0)