How can I clear the Parse.Object relationship records when retrieving the object?

The Relational Data JavaScript parsing tutorial states that

By default, when retrieving an object, the associated Parse.Objects is not implausible. The values โ€‹โ€‹of these objects cannot be retrieved until they have been retrieved.

They also state that when Parse.Object

a field exists relation

, the method should be used query().find()

. Example provided in the docs:

var user = Parse.User.current();
var relation = user.relation("likes");
relation.query().find({
  success: function(list) {
    // list contains the posts that the current user likes.
  }
});

      

I understand how good this is from an SDK design standpoint, because it prevents you from potentially grabbing hundreds of related records unnecessarily. Get the data you need at the moment.

But in my case, I know that there will never be a time where I have more than ten related records to be retrieved. And I want these records to be selected every time because they will show up in the view.

Is there a cleaner way to encapsulate this functionality by extending Parse.Object

?

+3


source to share


2 answers


Parse.Object

{Parse.Promise} fetch(options)

combined with Parse.Promise

always(callback)

are key.

We can override the method fetch

on extension Parse.Object

to always retrieve relationship objects.

For example, consider the following example, where we want to receive a post and its comments (let's assume that this happens inside a view that wants to display the post and its comments):

var Post = Parse.Object.extend("Post"),
    postsQuery = new Parse.Query(Post),
    myPost;

postsQuery.get("xWMyZ4YEGZ", {
  success: function(post) {
    myPost = post;
  }
).then(function(post) {
  post.relation("comments").query().find({
    success: function(comments) {
      myPost.comments = comments;
    }
  });
});

      

If we had to do this every time we wanted to receive a post and its comments, it would become very repetitive and very tedious. And we won't DRY, copy and paste like 15 lines of code every time.



So, let's encapsulate this by extending Parse.Object

and redefining its function fetch

, for example:

/*
  models/post.js
*/

window.myApp = window.myApp || {};

window.myApp.Post = Parse.Object.extend("Post", {
  fetch: function(options) {
    var _arguments = arguments;
    this.commentsQuery = this.relation("comments").query();
    return this.commentsQuery.find({
      success: (function(_this) {
        return function(comments) {
          return _this.comments = comments;
        };
      })(this)
    }).always((function(_this) {
      return function() {
        return _this.constructor.__super__.fetch.apply(_this, _arguments);
      };
    })(this));
  }
});

      

Disclaimer: You must really understand how closures and IIFEs are to fully understand how this works, but here's what happens when fetch is called on an existing one Post

at a descriptive level:

  • Trying to get comments on a post and set it to the post attribute comments

  • Regardless of the result of the above (whether it fails or not), the operation always performs the default fetch operation and calls all these callbacks
+3


source


Have you tried using include ("like")?

I'm not as familiar with the JavaScript API as the ObjC API .. so in the example below I'm not sure if "objectId" is the actual key name you need to use ...

var user = Parse.User.current();
var query = new Parse.Query(Parse.User);
query.equalTo(objectId, user.objectId);
query.include("likes")
query.find({
  success: function(user) {
    // Do stuff
  }
});

      



In general, you want to think about the opposite of your relationship. I'm not sure if it's a good idea to add custom value to the User object. Consider creating a Like type and specifying it instead of a user.

Example from Parse docs: https://parse.com/docs/js_guide#queries-relational

var query = new Parse.Query(Comment);

// Retrieve the most recent ones
query.descending("createdAt");

// Only retrieve the last ten
query.limit(10);

// Include the post data with each comment
query.include("post");

query.find({
  success: function(comments) {
    // Comments now contains the last ten comments, and the "post" field
    // has been populated. For example:
    for (var i = 0; i < comments.length; i++) {
      // This does not require a network access.
      var post = comments[i].get("post");
    }
  }
});

      

+3


source







All Articles