Skip to content

Commit cfcf48c

Browse files
committed
Merge pull request #258 from wangxiao/all-get
优化 AV.File,统一 API。
2 parents b5e7ece + ca3d5e1 commit cfcf48c

File tree

10 files changed

+336
-236
lines changed

10 files changed

+336
-236
lines changed

demo/test-es5.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ file.save().then(function (data) {
3131
console.log(data);
3232
}).catch(function (error) {
3333
console.log(error);
34+
});
35+
36+
// 查找文件
37+
var query = new AV.Query(TestClass);
38+
query.equalTo('name', 'hjiang');
39+
query.find().then(function (list) {
40+
console.log(list);
3441
});

demo/test-es6.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ file.save().then((data) => {
3131
console.log(error);
3232
});
3333

34+
// 查找文件
35+
const query = new AV.Query(TestClass);
36+
query.equalTo('name', 'hjiang');
37+
query.find().then((list) => {
38+
console.log(list);
39+
});
40+
41+

src/browserify-wrapper/ajax-browser.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,6 @@ const ajax = (method, url, data, success, error) => {
5959
}
6060
};
6161

62-
if (method.toLowerCase() === 'get') {
63-
let i = 0;
64-
for (let k in data) {
65-
if (i === 0) {
66-
url = url + '?';
67-
} else {
68-
url = url + '&';
69-
}
70-
url = url + k + '=' + encodeURIComponent(JSON.stringify(data[k]));
71-
i ++;
72-
}
73-
}
74-
7562
let headers = {
7663
'X-LC-Id': appId,
7764
'X-LC-UA': 'LC-Web-' + AV.version,
@@ -93,6 +80,24 @@ const ajax = (method, url, data, success, error) => {
9380
}
9481
}
9582

83+
if (method.toLowerCase() === 'get') {
84+
let i = 0;
85+
for (let k in data) {
86+
if (i === 0) {
87+
url = url + '?';
88+
} else {
89+
url = url + '&';
90+
}
91+
92+
if (typeof data[k] === 'object') {
93+
data[k] = JSON.stringify(data[k]);
94+
}
95+
96+
url = url + k + '=' + encodeURIComponent(data[k]);
97+
i ++;
98+
}
99+
}
100+
96101
if (masterKey && AV._useMasterKey) {
97102
headers['X-LC-Sign'] = sign(masterKey, true);
98103
} else {

src/browserify-wrapper/upload-browser.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77

88
module.exports = function upload(file, AV, saveOptions) {
99
//use /files endpoint.
10-
var self = file;
1110
var dataFormat;
12-
self._previousSave = self._source.then(function(data, type) {
11+
file._previousSave = file._source.then(function(data, type) {
1312
dataFormat = data;
14-
return self._qiniuToken(type);
13+
return file._qiniuToken(type);
1514
}).then(function(response) {
16-
self._url = response.url;
17-
self._bucket = response.bucket;
18-
self.id = response.objectId;
15+
file.attributes.url = response.url;
16+
file._bucket = response.bucket;
17+
file.id = response.objectId;
1918
//Get the uptoken to upload files to qiniu.
2019
var uptoken = response.token;
2120

2221
var data = new FormData();
23-
data.append("file", dataFormat);
24-
data.append('name', self._name);
25-
data.append("key", self._qiniu_key);
26-
data.append("token", uptoken);
22+
data.append('file', dataFormat);
23+
data.append('name', file.attributes.name);
24+
data.append('key', file._qiniu_key);
25+
data.append('token', uptoken);
2726

2827
var promise = new AV.Promise();
2928
var handled = false;
@@ -41,23 +40,23 @@ module.exports = function upload(file, AV, saveOptions) {
4140
}
4241
handled = true;
4342

44-
delete self._qiniu_key;
43+
delete file._qiniu_key;
4544
if (xhr.status >= 200 && xhr.status < 300) {
4645
var response;
4746
try {
4847
response = JSON.parse(xhr.responseText);
4948
} catch (e) {
5049
promise.reject(e);
51-
self.destroy();
50+
file.destroy();
5251
}
5352
if (response) {
54-
promise.resolve(self);
53+
promise.resolve(file);
5554
} else {
5655
promise.reject(response);
5756
}
5857
} else {
5958
promise.reject(xhr);
60-
self.destroy();
59+
file.destroy();
6160
}
6261
}
6362
};

src/browserify-wrapper/upload.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ var Promise = require('../promise');
1111

1212
module.exports =function upload(file) {
1313
file._previousSave = file._source.then(function(base64, type) {
14-
file._base64 = base64;
14+
file.attributes.base64 = base64;
1515
return file._qiniuToken(type);
1616
}).then(function(response) {
17-
file._url = response.url;
17+
file.attributes.url = response.url;
1818
file._bucket = response.bucket;
1919
file.id = response.objectId;
2020
//Get the uptoken to upload files to qiniu.
2121
var uptoken = response.token;
2222
var promise = new Promise();
2323
var extra = new qiniu.io.PutExtra();
24-
if(file._metaData.mime_type)
25-
extra.mimeType = file._metaData.mime_type;
26-
var body = new Buffer(file._base64, 'base64');
24+
if(file.attributes.metaData.mime_type)
25+
extra.mimeType = file.attributes.metaData.mime_type;
26+
var body = new Buffer(file.attributes.base64, 'base64');
2727
qiniu.io.put(uptoken, file._qiniu_key, body, extra, function(err, ret) {
2828
delete file._qiniu_key;
29-
delete file._base64;
29+
delete file.attributes.base64;
3030
if(!err) {
3131
promise.resolve(file);
3232
} else {

0 commit comments

Comments
 (0)