Skip to content

Commit 078a06c

Browse files
committed
Merge pull request #131 from killme2008/master
Added AV.Cloud.getServerDate()
2 parents b03fff4 + 5d1154c commit 078a06c

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

lib/cloudfunction.js

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

37+
/**
38+
* Make a call to request server date time.
39+
* @param {Object} options A Backbone-style options object
40+
* options.success, if set, should be a function to handle a successful
41+
* call to a cloud function. options.error should be a function that
42+
* handles an error running the cloud function. Both functions are
43+
* optional. Both functions take a single argument.
44+
* @return {AV.Promise} A promise that will be resolved with the result
45+
* of the function.
46+
* @since 0.5.9
47+
*/
48+
getServerDate: function(options) {
49+
var request = AV._request("date", null, null, 'GET');
50+
51+
return request.then(function(resp) {
52+
return AV._decode(null, resp);
53+
})._thenRunCallbacks(options);
54+
},
55+
3756
/**
3857
* Makes a call to request a sms code for operation verification.
3958
* @param {Object} data The mobile phone number string or a JSON

lib/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ module.exports = function(AV) {
354354
if (route !== "batch" &&
355355
route !== "classes" &&
356356
route !== "files" &&
357+
route !== "date" &&
357358
route !== "functions" &&
358359
route !== "login" &&
359360
route !== "push" &&

test/cloud.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
describe("AV.Cloud", function() {
2+
describe("#getServerDate", function(){
3+
this.timeout(10000);
4+
it("should return a date.", function(done){
5+
AV.Cloud.getServerDate().then(function(date) {
6+
expect(date).to.be.a('object');
7+
expect(date instanceof Date).to.be(true);
8+
done();
9+
}).catch(function(err) {
10+
throw err;
11+
});
12+
});
13+
});
14+
});

test/promise.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('promise', function() {
2525

2626

2727
describe('AV.Promise._debugError', function() {
28+
AV.Promise._isPromisesAPlusCompliant = true;
2829
it('should log error', function(done) {
2930
AV.Promise.setDebugError(true);
3031
var p = new AV.Promise();
@@ -34,6 +35,7 @@ describe('promise', function() {
3435
p.resolve(100);
3536
p.finally(function(){
3637
AV.Promise.setDebugError(false);
38+
AV.Promise._isPromisesAPlusCompliant = false;
3739
done();
3840
});
3941
});
@@ -198,6 +200,7 @@ describe('promise', function() {
198200
expect(error).to.be(1);
199201
//should be 1 ms
200202
expect(Date.now() - startDate).to.be.within(0, 10);
203+
AV.Promise._isPromisesAPlusCompliant = false;
201204
setTimeout(done, 500);
202205
}).done(function(ret){
203206
throw ret;
@@ -208,6 +211,7 @@ describe('promise', function() {
208211

209212
describe('PromiseAPlusCompliant', function() {
210213
it('should catch all them.', function(done) {
214+
AV.Promise._isPromisesAPlusCompliant = true;
211215
new AV.Promise(function(resolve, reject) {
212216
return resolve(123);
213217
})
@@ -216,11 +220,14 @@ describe('promise', function() {
216220
})
217221
.then(function(){}, function(error) {
218222
expect(error.code).to.be(1);
223+
AV.Promise._isPromisesAPlusCompliant = false;
219224
done();
220225
})
221226
});
222227

223228
it('shoud work in order', function(done) {
229+
AV.Promise._isPromisesAPlusCompliant = true;
230+
224231
this.timeout(10000);
225232
var ret = [];
226233
var a = new AV.Promise(function(resolve){
@@ -255,13 +262,15 @@ describe('promise', function() {
255262
done();
256263
}, 300);
257264
}, 500);
265+
AV.Promise._isPromisesAPlusCompliant = false;
258266
done();
259267
}, 300);
260268

261269
});
262270
});
263271

264272
describe('AV.Promise.race', function(){
273+
AV.Promise._isPromisesAPlusCompliant = true;
265274
it('should be called once.', function(done) {
266275
var wasCalled = false;
267276
AV.Promise.race([
@@ -273,10 +282,12 @@ describe('promise', function() {
273282
if (wasCalled) throw 'error';
274283
wasCalled = true;
275284
expect(value).to.be(1);
285+
AV.Promise._isPromisesAPlusCompliant = false;
276286
done();
277287
});
278288
});
279289
it('should run all promises.', function(done) {
290+
AV.Promise._isPromisesAPlusCompliant = true;
280291
var results = [];
281292
var timerPromisefy2 = function(delay) {
282293
return new AV.Promise(function (resolve, reject) {
@@ -307,6 +318,7 @@ describe('promise', function() {
307318
expect(results[1]).to.be(32);
308319
expect(results[2]).to.be(64);
309320
expect(results[3]).to.be(128);
321+
AV.Promise._isPromisesAPlusCompliant = false;
310322
done();
311323
}, 500);
312324
});

0 commit comments

Comments
 (0)