检索父加上儿童相关的一个解析REST API调用 [英] Retrieving parent plus related children in one Parse REST API call

查看:220
本文介绍了检索父加上儿童相关的一个解析REST API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个小应用程序,我需要做的第一件事就是拨打电话使用AngularJS的ngResource解析的REST API。

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.

我已经成功地构建了两个解析类(我们称他们为父与子现在),并成立了一个多到很多人的关系,使得家长可以与零-N子对象和一个孩子可能会涉及到零-N的父对象。

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.

所以,我们试试这个:

.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?

编辑:请注意,虽然我已经设置了父母与子女的关系了,我很开放的建议,如果这不是,如果我需要查询这样的数据做事情的最好方法。这里将是一个集的父对象(让我们100说下)的限制和一组可能的子对象的要小得多(30岁以下)。

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).

推荐答案

从DANH的帮助下,我已经摸索出如何得到这个成立于我想要的方式。这里有一个小code片断:

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.
    }
});

首先,在Parse.com数据浏览器中创建两个类。确保父类有定义的数组列(在这种情况下,它被称为孩子们)。

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').

添加您的父对象ID和子对象ID的相关变量在脚本并运行它(显然包括在脚本的顶部您的API密钥)。嘿preSTO!你会为每次运行的脚本关系的指针数组。

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.

不仅如此,我可以证实,我AngularJS REST API调用一个调用返回的家长和孩子的详细信息。

Not only that, I can confirm that my AngularJS REST Api call returns the parent and child details in one call.

这篇关于检索父加上儿童相关的一个解析REST API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆