Repository: parse-community/Parse-SDK-JS (JavaScript)

Issue:
Add isDataAvailable function to help determine if object is only pointer.
see: https://github.com/parse-community/parse-php-sdk/blob/master/src/Parse/ParseObject.php#L475

for the PHP implementation.

Should be able to do:

```
if(!object.isDataAvailable()) {
    await object.fetch();
}
```



Discussion:

Patch:
diff --git a/src/ParseObject.js b/src/ParseObject.js
index cdd615642..2f8fa6947 100644
--- a/src/ParseObject.js
+++ b/src/ParseObject.js
@@ -524,6 +524,15 @@ class ParseObject {
     return Object.keys(keys);
   }
 
+  /**
+   * Returns true if the object has been fetched.
+   * @return {Boolean}
+   */
+  isDataAvailable(): boolean {
+    const serverData = this._getServerData();
+    return !!Object.keys(serverData).length;
+  }
+
   /**
    * Gets a Pointer referencing this Object.
    * @return {Pointer}
@@ -1980,7 +1989,7 @@ const DefaultController = {
             'All objects must have an ID'
           );
         }
-        if (forceFetch || Object.keys(el._getServerData()).length === 0) {
+        if (forceFetch || !el.isDataAvailable()) {
           ids.push(el.id);
           objs.push(el);
         }


Test patch:
diff --git a/integration/test/ParseObjectTest.js b/integration/test/ParseObjectTest.js
index 7877a36c6..47df1124f 100644
--- a/integration/test/ParseObjectTest.js
+++ b/integration/test/ParseObjectTest.js
@@ -1663,4 +1663,37 @@ describe('Parse Object', () => {
 
     done();
   });
+
+  it('isDataAvailable', async () => {
+    const child = new TestObject({ foo: 'bar' });
+    assert.equal(child.isDataAvailable(), false);
+
+    const parent = new TestObject({ child });
+    await parent.save();
+
+    assert.equal(child.isDataAvailable(), true);
+    assert.equal(parent.isDataAvailable(), true);
+
+    const query = new Parse.Query(TestObject);
+    const fetched = await query.get(parent.id);
+    const unfetched = fetched.get('child');
+
+    assert.equal(fetched.isDataAvailable(), true);
+    assert.equal(unfetched.isDataAvailable(), false);
+  });
+
+  it('isDataAvailable user', async () => {
+    let user = new Parse.User();
+    user.set('username', 'plain');
+    user.set('password', 'plain');
+    await user.signUp();
+    assert.equal(user.isDataAvailable(), true);
+
+    user = await Parse.User.logIn('plain', 'plain');
+    assert.equal(user.isDataAvailable(), true);
+
+    const query = new Parse.Query(Parse.User);
+    const fetched = await query.get(user.id);
+    assert.equal(fetched.isDataAvailable(), true);
+  });
 });
diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js
index e27a14fdd..8b7429866 100644
--- a/src/__tests__/ParseObject-test.js
+++ b/src/__tests__/ParseObject-test.js
@@ -916,6 +916,18 @@ describe('ParseObject', () => {
     expect(p.op('age')).toBe(undefined);
   });
 
+  it('isDataAvailable', () => {
+    const p = new ParseObject('Person');
+    p.id = 'isdataavailable';
+    p.set('age', 24);
+    expect(p.isDataAvailable()).toBe(false);
+    const updated = new Date();
+    p._handleSaveResponse({
+      updatedAt: { __type: 'Date', iso: updated.toISOString() }
+    });
+    expect(p.isDataAvailable()).toBe(true);
+  });
+
   it('handles ACL when saved', () => {
     const p = new ParseObject('Person');