Jon
Jon

Reputation: 205

Hooks or filters for `Parse.Object`?

Right now I have two sets of data: Post and Images. Every time I fetch a Post I'm having to do a sub-query to grab its image. I'd like to just perform that query to grab the image every time the Post object is initialized. That way every time I have a Post object, I know I also have access to its image. Does this make sense?

Is there any way to do this? The initialize function doesn't give me access to any of the object's data because it's called before it's set.

Upvotes: 1

Views: 46

Answers (1)

Ryan Kreager
Ryan Kreager

Reputation: 3581

What you are looking for is the .include() function. It allows you to name a related object via it's field and have it returned with the parent object. Like so:

var Post = Parse.Object.extend("Post");
var query = new Parse.Query(Post);
query.where("author", "Mr. Lebowski");

// Include images associated with the Post
query.include("images");

query.find({
  success: function(posts) {
    // Images have been retrieved. For example:
    for (var i = 0; i < posts.length; i++) {
      var Images = i.images; //This will be populated with no sub-query
    }    
  }
});

Upvotes: 1

Related Questions