Skip to content

Commit 8374093

Browse files
aiskleeyeh
authored andcommitted
feat: add AV.Object.query (#502)
1 parent 7b66203 commit 8374093

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/object.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ module.exports = function(AV) {
14101410
var newArguments = [className].concat(_.toArray(arguments));
14111411
return AV.Object.extend.apply(NewClassObject, newArguments);
14121412
};
1413-
NewClassObject['new'] = function(attributes, options){
1413+
NewClassObject['new'] = function(attributes, options) {
14141414
return new NewClassObject(attributes, options);
14151415
};
14161416
AV.Object._classMap[className] = NewClassObject;
@@ -1419,7 +1419,7 @@ module.exports = function(AV) {
14191419

14201420
// ES6 class syntax support
14211421
Object.defineProperty(AV.Object.prototype, 'className', {
1422-
get: function(){
1422+
get: function() {
14231423
const className = this._className || this.constructor._LCClassName || this.constructor.name;
14241424
// If someone tries to subclass "User", coerce it to the right type.
14251425
if (className === "User") {
@@ -1453,6 +1453,12 @@ module.exports = function(AV) {
14531453
AV.Object._classMap[className] = klass;
14541454
};
14551455

1456+
Object.defineProperty(AV.Object, 'query', {
1457+
get() {
1458+
return new AV.Query(this.prototype.className);
1459+
},
1460+
});
1461+
14561462
AV.Object._findUnsavedChildren = function(object, children, files) {
14571463
AV._traverse(object, function(object) {
14581464
if (object instanceof AV.Object) {

src/utils/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ const inherits = function inherits(parent, protoProps, staticProps) {
107107
}
108108

109109
// Inherit class (static) properties from parent.
110-
_.extend(child, parent);
110+
for (const prop of Object.getOwnPropertyNames(parent)) {
111+
const propertyDescriptor = Object.getOwnPropertyDescriptor(parent, prop);
112+
Object.defineProperty(child, prop, propertyDescriptor);
113+
}
111114

112115
// Set the prototype chain to inherit from `parent`, without calling
113116
// `parent`'s constructor function.

storage.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ declare namespace AV {
267267
attributes: any;
268268
changed: boolean;
269269
className: string;
270+
query: Query;
270271

271272
constructor(className?: string, options?: any);
272273
constructor(attributes?: string[], options?: any);

test/object.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,26 @@ describe('Objects', function(){
304304
});
305305
});
306306

307+
describe('query', () => {
308+
it('should get AV.Query instance', () => {
309+
const Foo = AV.Object.extend('Foo');
310+
expect(Foo.query.className).to.be('Foo');
311+
const Bar = Foo.extend('Bar');
312+
expect(Bar.query.className).to.be('Bar');
313+
});
314+
it('should get AV.Query instance with ES6 class', () => {
315+
class Foo extends AV.Object {}
316+
expect(Foo.query.className).to.be('Foo');
317+
class Bar extends Foo {}
318+
expect(Bar.query.className).to.be('Bar');
319+
});
320+
});
321+
307322
describe("Retrieving Objects",function(){
308323
it("should be the just save Object",function(){
309324
var GameScore = AV.Object.extend("GameScore");
310-
var query = new AV.Query(GameScore);
311325
debug(objId);
312-
return query.get(objId).then(function(result) {
326+
return GameScore.query.get(objId).then(function(result) {
313327
expect(gameScore.id).to.be.ok();
314328
expect(gameScore.get('objectId')).to.be(gameScore.id);
315329
expect(gameScore.get('id')).to.be('id');

0 commit comments

Comments
 (0)