Skip to content

Commit 710d488

Browse files
committed
Merge pull request #276 from jysperm/fix-fetch
fix(Object.fetch): options 默认为空对象、允许 include 选项为数组
2 parents d8767c4 + c8423f0 commit 710d488

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/object.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,17 @@ module.exports = function(AV) {
795795
* completes.
796796
*/
797797
fetch: function() {
798-
var options = null;
798+
var options = {};
799799
var fetchOptions = {};
800800
if(arguments.length === 1) {
801801
options = arguments[0];
802802
} else if(arguments.length === 2) {
803803
fetchOptions = arguments[0];
804-
options = arguments[1];
804+
options = arguments[1] || {};
805+
}
806+
807+
if (fetchOptions && fetchOptions.include && fetchOptions.include.length > 0) {
808+
fetchOptions.include = fetchOptions.include.join(',');
805809
}
806810

807811
var self = this;

test/object.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,19 @@ describe('Objects', function(){
350350
});
351351
});
352352

353+
it('should fetch include authors successfully', function() {
354+
var myPost = new Post();
355+
myPost.set('author1', new Person({name: '1'}));
356+
myPost.set('author2', new Person({name: '2'}));
357+
return myPost.save().then(function() {
358+
myPost = AV.Object.createWithoutData('Post', myPost.id);
359+
return myPost.fetch({include: ['author1', 'author2']}, {}).then(function() {
360+
expect(myPost.get('author1').get('name')).to.be('1');
361+
expect(myPost.get('author2').get('name')).to.be('2');
362+
});
363+
});
364+
});
365+
353366
/*
354367
355368
it("it should fetch relation post",function(done){

0 commit comments

Comments
 (0)