Skip to content

Commit 7f5c497

Browse files
authored
feat(Object): allow user to specify the name when registering class (#418)
1 parent f0788d2 commit 7f5c497

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/object.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ module.exports = function(AV) {
12261226
};
12271227
/**
12281228
* Delete objects in batch.The objects className must be the same.
1229-
* @param {Array} The <code>AV.Object</code> array to be deleted.
1229+
* @param {AV.Object[]} objects The <code>AV.Object</code> array to be deleted.
12301230
* @param {AuthOptions} options
12311231
* @return {Promise} A promise that is fulfilled when the save
12321232
* completes.
@@ -1385,7 +1385,7 @@ module.exports = function(AV) {
13851385
// ES6 class syntax support
13861386
Object.defineProperty(AV.Object.prototype, 'className', {
13871387
get: function(){
1388-
const className = this._className || this.constructor.name;
1388+
const className = this._className || this.constructor._LCClassName || this.constructor.name;
13891389
// If someone tries to subclass "User", coerce it to the right type.
13901390
if (className === "User") {
13911391
return "_User";
@@ -1394,14 +1394,27 @@ module.exports = function(AV) {
13941394
},
13951395
});
13961396

1397-
AV.Object.register = klass => {
1397+
/**
1398+
* Register a class.
1399+
* If a subclass of <code>AV.Object</code> is defined with your own implement
1400+
* rather then <code>AV.Object.extend</code>, the subclass must be registered.
1401+
* @param {Function} klass A subclass of <code>AV.Object</code>
1402+
* @param {String} [name] Specify the name of the class. Useful when the class might be uglified.
1403+
* @example
1404+
* class Person extend AV.Object {}
1405+
* AV.Object.register(Person);
1406+
*/
1407+
AV.Object.register = (klass, name) => {
13981408
if (!(klass.prototype instanceof AV.Object)) {
13991409
throw new Error('registered class is not a subclass of AV.Object');
14001410
}
1401-
const className = klass.name;
1411+
const className = name || klass.name;
14021412
if (!className.length) {
14031413
throw new Error('registered class must be named');
14041414
}
1415+
if (name) {
1416+
klass._LCClassName = name;
1417+
}
14051418
AV.Object._classMap[className] = klass;
14061419
};
14071420

test/object.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Post=AV.Object.extend("Post");
66

77
// for #extend test
88
class Person extends AV.Object {}
9+
class UglifiedClass extends AV.Object {}
910
var BackbonePerson = AV.Object.extend('Person');
1011

1112
describe('Objects', function(){
@@ -57,12 +58,18 @@ describe('Objects', function(){
5758
expect(backbonePerson._toFullJSON()).to.eql(es6Person._toFullJSON());
5859
});
5960

60-
it('#resiger an ES6 class', () => {
61+
it('#register an ES6 class', () => {
6162
expect(new AV.Object('Person')).to.be.a(BackbonePerson);
6263
AV.Object.register(Person);
6364
expect(new AV.Object('Person')).to.be.a(Person);
6465
expect(() => AV.Object.register(1)).to.throwError();
6566
expect(() => AV.Object.register(function(){})).to.throwError();
67+
});
68+
69+
it('#register with name', () => {
70+
AV.Object.register(UglifiedClass, 'RealClass');
71+
expect(new AV.Object('RealClass')).to.be.a(UglifiedClass);
72+
expect(AV._encode(new UglifiedClass()).className).to.equal('RealClass');
6673
})
6774
});
6875

0 commit comments

Comments
 (0)