Skip to content

Commit 9d0ad83

Browse files
committed
fix(Object): correct #createWithoutData return type
fixes #566
1 parent 8b47007 commit 9d0ad83

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/object.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,15 +1387,23 @@ module.exports = function(AV) {
13871387
/**
13881388
* Creates an instance of a subclass of AV.Object for the give classname
13891389
* and id.
1390-
* @param {String} className The name of the AV class backing this model.
1390+
* @param {String|Function} class the className or a subclass of AV.Object.
13911391
* @param {String} id The object id of this model.
13921392
* @return {AV.Object} A new subclass instance of AV.Object.
13931393
*/
1394-
AV.Object.createWithoutData = function(className, id, hasData) {
1395-
var result = new AV.Object(className);
1396-
result.id = id;
1397-
result._hasData = hasData;
1398-
return result;
1394+
AV.Object.createWithoutData = (klass, id, hasData) => {
1395+
let _klass;
1396+
if (_.isString(klass)) {
1397+
_klass = AV.Object._getSubclass(klass);
1398+
} else if (klass.prototype && klass.prototype instanceof AV.Object) {
1399+
_klass = klass;
1400+
} else {
1401+
throw new Error('class must be a string or a subclass of AV.Object.');
1402+
}
1403+
const object = new _klass();
1404+
object.id = id;
1405+
object._hasData = hasData;
1406+
return object;
13991407
};
14001408
/**
14011409
* Delete objects in batch.

test/object.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ describe('Objects', function() {
4949
for (var i = 100000; i > 0; i--) {
5050
Test = AV.Object.extend('Test');
5151
}
52-
new Test();
52+
const test = new Test();
53+
expect(test.className).to.eql('Test');
5354
});
5455

5556
it('ES6 extend syntex', () => {
@@ -61,6 +62,15 @@ describe('Objects', function() {
6162
expect(backbonePerson._toFullJSON()).to.eql(es6Person._toFullJSON());
6263
});
6364

65+
it('createWithoutData', () => {
66+
expect(AV.Object.createWithoutData(Person, 'id') instanceof Person).to.be(
67+
true
68+
);
69+
expect(
70+
AV.Object.createWithoutData('Person', 'id') instanceof BackbonePerson
71+
).to.be(true);
72+
});
73+
6474
it('#register an ES6 class', () => {
6575
expect(new AV.Object('Person')).to.be.a(BackbonePerson);
6676
AV.Object.register(Person);

0 commit comments

Comments
 (0)