Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 2a89352

Browse files
committed
Equivalent for Parse.Query.count(), for now not backed by a store
1 parent 6422e49 commit 2a89352

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ParsePatches.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ var patches = {
6161
return this.equalTo('objectId', objectId).limit(1);
6262
},
6363

64+
/**
65+
* The ParseReact equivalent to Parse.Query(...).count()
66+
*/
67+
observeCount: function() {
68+
this._observeCount = true;
69+
return this;
70+
},
71+
6472
/**
6573
* Patches for Parse.User to watch for user signup / login / logout
6674
*/
@@ -99,6 +107,9 @@ var ParsePatches = {
99107
if (!Parse.Query.prototype.observeOne) {
100108
Parse.Query.prototype.observeOne = patches.observeOne;
101109
}
110+
if (!Parse.Query.prototype.observeCount) {
111+
Parse.Query.prototype.observeCount = patches.observeCount;
112+
}
102113
pointerMethods.forEach(function(method) {
103114
var old = Parse.Query.prototype[method];
104115
Parse.Query.prototype[method] = function(attr, value) {

src/Subscription.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ class Subscription {
113113
if (resultSet[0] && !(resultSet[0] instanceof Id)) {
114114
resultSet = resultSet.map(extractId);
115115
}
116-
var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : [];
117-
callbacks.onNext(this.originalQuery._observeOne ? data[0] : data);
116+
if (!this.originalQuery._observeCount) {
117+
var data = resultSet.length ? ObjectStore.getDataForIds(resultSet) : [];
118+
callbacks.onNext(this.originalQuery._observeOne ? data[0] : data);
119+
}
118120

119121
return oid;
120122
}
@@ -140,6 +142,16 @@ class Subscription {
140142
*/
141143
issueQuery() {
142144
this.pending = true;
145+
if (this.originalQuery._observeCount) {
146+
this.originalQuery.count().then((result) => {
147+
this.pending = false;
148+
this._forEachSubscriber((subscriber) => subscriber.onNext(result));
149+
}, (err) => {
150+
this.pending = false;
151+
this.pushError(err);
152+
});
153+
return;
154+
}
143155
this.originalQuery.find().then((results) => {
144156
this.pending = false;
145157
this.resultSet = ObjectStore.storeQueryResults(

0 commit comments

Comments
 (0)