Skip to content

Commit ac51fa0

Browse files
committed
添加 AV.Cloud.rpc 方法
1 parent 5273682 commit ac51fa0

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

lib/cloudfunction.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ module.exports = function(AV) {
3434
})._thenRunCallbacks(options);
3535
},
3636

37+
rpc: function(name, data, options) {
38+
if (_.isArray(data))
39+
return console.warn('Can\'t pass Array as the param of rpc function in JavaScript SDK.');
40+
41+
return AV._request('call', name, null, 'POST', AV._encodeObjectOrArray(data)).then(function(resp) {
42+
return AV._decode('', resp).result;
43+
})._thenRunCallbacks(options);
44+
},
45+
3746
/**
3847
* Make a call to request server date time.
3948
* @param {Object} options A Backbone-style options object

lib/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ module.exports = function(AV) {
277277
route !== "files" &&
278278
route !== "date" &&
279279
route !== "functions" &&
280+
route !== "call" &&
280281
route !== "login" &&
281282
route !== "push" &&
282283
route !== "search/select" &&
@@ -527,6 +528,26 @@ module.exports = function(AV) {
527528
return value;
528529
};
529530

531+
AV._encodeObjectOrArray = function(value) {
532+
var encodeAVObject = function(object) {
533+
if (object && object._toFullJSON){
534+
object = object._toFullJSON([]);
535+
}
536+
537+
return _.mapObject(object, function(value) {
538+
return AV._encode(value, []);
539+
});
540+
};
541+
542+
if (_.isArray(value)) {
543+
return value.map(function(object) {
544+
return encodeAVObject(object);
545+
});
546+
} else {
547+
return encodeAVObject(value);
548+
}
549+
};
550+
530551
AV._arrayEach = AV._.each;
531552

532553
/**

test/cloud.js

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
describe("AV.Cloud", function() {
2+
var originalServerURL, originalAppId, originalAppKey, originalUseMasterKey;
3+
4+
before(function() {
5+
originalServerURL = AV.serverURL;
6+
originalAppId = AV.applicationId;
7+
originalAppKey = AV.applicationKey;
8+
originalUseMasterKey = AV._useMasterKey;
9+
10+
AV.serverURL = 'https://leancloud.cn'
11+
AV.applicationId = '4h2h4okwiyn8b6cle0oig00vitayum8ephrlsvg7xo8o19ne';
12+
AV.applicationKey = '3xjj1qw91cr3ygjq9lt0g8c3qpet38rrxtwmmp0yffyoy2t4';
13+
AV._useMasterKey = false;
14+
15+
AV.User._currentUser = null;
16+
});
17+
218
describe("#getServerDate", function(){
3-
419
it("should return a date.", function(done){
520
AV.Cloud.getServerDate().then(function(date) {
621
expect(date).to.be.a('object');
@@ -11,4 +26,123 @@ describe("AV.Cloud", function() {
1126
});
1227
});
1328
});
29+
30+
describe('#rpc', function() {
31+
it('receive complex object', function(done) {
32+
AV.Cloud.rpc('complexObject', null, {
33+
success: function(result) {
34+
expect(result.foo).to.be('bar');
35+
expect(result.t).to.be.a(Date);
36+
expect(result.t.toISOString()).to.be('2015-05-14T09:21:18.273Z');
37+
38+
expect(result.avObject).to.be.a(AV.Object);
39+
expect(result.avObject.className).to.be('ComplexObject');
40+
expect(result.avObject.get('numberColumn')).to.be(1.23);
41+
expect(result.avObject.get('arrayColumn')).to.eql([1, 2, 3]);
42+
expect(result.avObject.get('objectColumn')).to.eql({foo: 'bar'});
43+
expect(result.avObject.get('stringColumn')).to.be('testString');
44+
expect(result.avObject.get('anyColumn')).to.be('');
45+
expect(result.avObject.get('booleanColumn')).to.be(true);
46+
expect(result.avObject.get('pointerColumn')).to.be.a(AV.Object);
47+
expect(result.avObject.get('pointerColumn').id).to.be('55069e5be4b0c93838ed8e6c');
48+
expect(result.avObject.get('relationColumn')).to.be.a(AV.Relation);
49+
expect(result.avObject.get('relationColumn').targetClassName).to.be('TestObject');
50+
expect(result.avObject.get('geopointColumn')).to.be.a(AV.GeoPoint);
51+
expect(result.avObject.get('geopointColumn').latitude).to.be(0);
52+
expect(result.avObject.get('geopointColumn').longitude).to.be(30);
53+
expect(result.avObject.get('dateColumn')).to.be.a(Date);
54+
expect(result.avObject.get('dateColumn').toISOString()).to.be('2015-05-14T06:24:47.000Z');
55+
expect(result.avObject.get('fileColumn')).to.be.a(AV.File);
56+
expect(result.avObject.get('fileColumn').name()).to.be('ttt.jpg');
57+
expect(result.avObject.get('fileColumn').url()).to.be('http://ac-4h2h4okw.clouddn.com/4qSbLMO866Tf4YtT9QEwJwysTlHGC9sMl7bpTwhQ.jpg');
58+
59+
result.avObjects.forEach(function(object) {
60+
expect(object).to.be.a(AV.Object);
61+
expect(object.className).to.be('ComplexObject');
62+
});
63+
64+
done();
65+
},
66+
error: done
67+
});
68+
});
69+
70+
it('receive bare AVObject', function(done) {
71+
AV.Cloud.rpc('bareAVObject', null, {
72+
success: function(result) {
73+
expect(result).to.be.a(AV.Object);
74+
expect(result.className).to.be('ComplexObject');
75+
expect(result.get('fileColumn')).to.be.a(AV.File);
76+
done();
77+
},
78+
error: done
79+
});
80+
});
81+
82+
it('receive array of AVObjects', function(done) {
83+
AV.Cloud.rpc('AVObjects', null, {
84+
success: function(result) {
85+
result.forEach(function(object) {
86+
expect(object).to.be.a(AV.Object);
87+
expect(object.className).to.be('ComplexObject');
88+
});
89+
done();
90+
},
91+
error: done
92+
})
93+
});
94+
95+
it('send AVObject', function(done) {
96+
var avObject = new AV.Object('ComplexObject');
97+
var avObjectItem = new AV.Object('ComplexObject');
98+
99+
avObject.set({
100+
name: 'avObject',
101+
pointerColumn: AV.Object.createWithoutData('_User', '55069e5be4b0c93838ed8e6c')._toPointer()
102+
});
103+
104+
avObjectItem.set({
105+
name: 'avObjects'
106+
});
107+
108+
AV.Object.saveAll([avObject, avObjectItem], {
109+
success: function() {
110+
AV.Cloud.rpc('testAVObjectParams', {
111+
avObject: avObject,
112+
avFile: AV.File.withURL('hello.txt', 'http://ac-1qdney6b.qiniudn.com/3zLG4o0d27MsCQ0qHGRg4JUKbaXU2fiE35HdhC8j.txt'),
113+
avObjects: [avObjectItem]
114+
}, {
115+
success: function() {
116+
done()
117+
},
118+
error: done
119+
});
120+
},
121+
error: done
122+
});
123+
});
124+
125+
it('send bare AVObject', function(done) {
126+
var avObject = new AV.Object('ComplexObject');
127+
128+
avObject.set({
129+
name: 'avObject',
130+
avFile: AV.File.withURL('hello.txt', 'http://ac-1qdney6b.qiniudn.com/3zLG4o0d27MsCQ0qHGRg4JUKbaXU2fiE35HdhC8j.txt')
131+
});
132+
133+
AV.Cloud.rpc('testBareAVObjectParams', avObject, {
134+
success: function() {
135+
done()
136+
},
137+
error: done
138+
});
139+
});
140+
});
141+
142+
after(function() {
143+
AV.serverURL = originalServerURL;
144+
AV.applicationId = originalAppId;
145+
AV.applicationKey = originalAppKey;
146+
AV._useMasterKey = originalUseMasterKey;
147+
});
14148
});

test/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<script src="status.js"></script>
3737
<script src="sms.js"></script>
3838
<script src="search.js"></script>
39+
<script src="cloud.js"></script>
3940
<script>
4041
onload = function(){
4142
mocha.checkLeaks();

0 commit comments

Comments
 (0)