I'm building out a little app and the first thing I need to do is make a call to Parse's REST API using AngularJS's ngResource.
I've successfully constructed two Parse classes (let's call them "Parent" and "Child" for now) and set up a many-to-many relation between them such that a Parent can relate to zero-n Child objects and a Child may relate to zero-n Parent objects.
So far, the very basic stuff works fine. This method, for example, successfully retrieves all the Parent objects:
.factory('Parent', function($resource, appConfig) {
return $resource('https://api.parse.com/1/classes/Parent/:id', {}, {
list : {
method : 'GET',
headers: appConfig.parseHttpsHeaders
}
});
})
Awesome. It even has a "Child" attribute describing the relation (as you would expect) but does not return the actual Child objects inside each Parent.
Now I need to expand on this method to retrieve the related Child objects at the same time - I really don't want to have to make another call per Parent to get the related Child objects.
So we try this:
.factory('Parent', function($resource, appConfig) {
return $resource('https://api.parse.com/1/classes/Parent/:id', {}, {
list : {
method : 'GET',
headers: appConfig.parseHttpsHeaders,
params : {
'include' : 'Child'
}
}
});
})
...and the server returns...the exact same response as before. The Child records are not included.
Any ideas where I'm going wrong?
Edit: Note that while I've set up the Parent and Child relation already, I'm open to suggestions if this is not the best way of doing things if I need to query the data in this way. There will be a limited set of Parent objects (let's say under 100) and a much smaller set of possible Child objects (under 30).
With help from danh, I've worked out how to get this set up in the way I wanted. Here's a little code snippet:
var cId = 'CHILD_ID';
var pId = 'PARENT_ID';
var Child = Parse.Object.extend("child");
var cQ = new Parse.Query(Child);
var Parent = Parse.Object.extend("parent");
var pQ = new Parse.Query(Parent);
pQ.get(pId, {
success: function (mResponse) {
console.log(pResponse);
cQ.get(cId, {
success: function (cResponse) {
console.log(cResponse);
pResponse.add('children', cResponse);
pResponse.save();
},
error: function (object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
},
error: function (object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
}
});
First of all, create your two classes in the Parse.com data browser. Ensure that the Parent class has an Array column defined (in this case it was called 'children').
Add your parent object id and child object id to the relevant variables in the script and run it (obviously including your API keys at the top of the script). Hey presto! You will have an array of pointers for each relationship you run the script for.
Not only that, I can confirm that my AngularJS REST Api call returns the parent and child details in one call.