简体   繁体   English

Ember Data嵌套模型

[英]Ember Data nested Models

I'm using EmberJs and Ember-Data in a Google App Engine project which uses NDB. 我在使用NDB的Google App Engine项目中使用EmberJs和Ember-Data。 In the database I have Host, Probe and Check entities. 在数据库中,我有Host,Probe和Check实体。 The database model doesn't really matter as long as I have my REST api in order but for clarity here are my database Classes: 只要我的REST api按顺序排列,数据库模型并不重要,但为了清楚起见,这里是我的数据库类:

class Host(ndb.Model):
    hostName = ndb.StringProperty()

hostKey = ndb.Key('Host', 'SomeHostId')

class Probe(ndb.Model):
    checkName = ndb.StringProperty()

probeKey = ndb.Key('Host', 'SomeHostId', 'Probe', 'SomeProbeId')

class Check(ndb.Model):
    checkName = ndb.StringProperty()

checkKey = ndb.Key('Host', 'SomeHostId', 'Probe', 'SomeProbeId', 'Check', 'SomeCheckId')

I've added the keys in order to show that each host has some probes running on them and each probe performs some checks. 我添加了密钥以显示每个主机上都运行了一些探针,每个探针都执行一些检查。

  • Host 主办
    • Probe 探测
      • Check 校验

In my App.Js I have defined the following models: 在我的App.Js中,我定义了以下模型:

App.Host = DS.Model.extend({
    hostName: DS.attr('string')
    probes: DS.hasMany('probe',{async:true})
});

App.Probe = DS.Model.extend({
    host: DS.belongsTo('host'),
    probeName: DS.attr('string')
    checks: DS.hasMany('check',{async:true})
});

App.Check = DS.Model.extend({
    probe: DS.belongsTo('probe'),
    hostName: DS.attr('string')
});

I have defined the following router: 我已经定义了以下路由器:

App.Router.map(function() {
    this.resource('hosts', function(){
        this.resource('host', { path:':host_id'}, function(){
            this.resource('probes', function(){
                this.resource('probe', { path:':probe_id'}, function(){
                    this.resource('checks', function(){
                        this.resource('check', { path:':check_id'}, function(){

                        });
                    });
                });
            });
        });
    });
});

And in AppEngine if have built the following URL paths: 在AppEngine中,如果已构建以下URL路径:

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    webapp2.Route('/hosts', HostsHandler),
    webapp2.Route('/hosts/<hostId>/', HostHandler),
    webapp2.Route('/hosts/<hostId>/probes', ProbesHandler),
    webapp2.Route('/hosts/<hostId>/probes/<probeId>/checks', ChecksHandler),
    webapp2.Route('/hosts/<hostId>/probes/<probeId>/checks/<checkId>/', CheckHandler)
])

http://example.com/hosts returns: http://example.com/hosts返回:

{
    "hosts": [
        {
            "hostName": "SomeHostName1",
            "id": "SomeHostId1"
        },
        {
            "hostName": "SomeHostName2",
            "id": "SomeHostId2"
        }
    ]
}

http://example.com/hosts/SomeHostId1/probes returns: http://example.com/hosts/SomeHostId1/probes返回:

{
    "probes": [
        {
            "probeName": "SomeProbeName1",
            "id": "SomeProbeId1",
            "host_id": "SomeHostId1"
        },
        {
            "probeName": "SomeProbeName2",
            "id": "SomeProbeId2",
            "host_id": "SomeHostId1"
        }
    ]
}

http://example.com/hosts/SomeHostId1/probes/SomeProbeId1/checks returns: http://example.com/hosts/SomeHostId1/probes/SomeProbeId1/checks返回:

{
    "checks": [
        {
            "checkName": "SomeCheckName1",
            "id": "SomeCheckId1",
            "probe_id": "SomeProbeId1"
        },
        {
            "checkName": "SomeCheckName2",
            "id": "SomeCheckId2",
            "probe_id": "SomeProbeId1"
        }
    ]
}

My templates are: 我的模板是:

<script type="text/x-handlebars" id="host">
  <h3>{{hostName}}</h3>
  {{#link-to 'probes' probes}}probes{{/link-to}}

  {{outlet}}
</script>

<script type="text/x-handlebars" id="probes">
  {{#each probe in probes}}
    Probe: {{probe.probeName}}
    {{#link-to 'checks' probe.checks}}checks{{/link-to}}
  {{/each}}

  {{outlet}}
</script>

<script type="text/x-handlebars" id="checks">
  {{#each check in checks}}
    Check: {{check.checkName}}
  {{/each}}
</script>

Now I have all this... but no clue how to tie it up together so that Ember-Data makes the right http requests. 现在我已经拥有了所有这些...但不知道如何将它们组合在一起,以便Ember-Data提供正确的http请求。 So far I've only seen request go to http://example.com/modelName/ 到目前为止我只看到请求访问http://example.com/modelName/

Currently Ember Data does not support this type of nested routes for API endpoints. 目前,Ember Data不支持API端点的此类嵌套路由。 There's been some talk about this, but it doesn't seem to be making any forward progress. 有一些关于此的讨论,但它似乎没有取得任何进展。

I don't know anything about App engine, but if you could obtain a config like this, for ember-data rest adapter 我对App引擎一无所知,但如果你能获得这样的配置,那就是ember-data rest adapter

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    webapp2.Route('/hosts', HostsHandler),
    webapp2.Route('/hosts/<hostId>', HostHandler),
    webapp2.Route('/probes', ProbesHandler),
    webapp2.Route('/probes/<probeId>', ProbesHandler),
    webapp2.Route('/checks/', CheckHandler)
    webapp2.Route('/checks/<checkId>/', CheckHandler)
])

And the response to http://example.com/hosts should return a json array hosts:[{},{}] and to http://example.com/hosts/1 a json representing a host object host:{} and the same for the other AppEngine routes http://example.com/hosts的响应应返回json数组主机:[{},{}]和http://example.com/hosts/1表示主机对象主机的json:{}和其他AppEngine路线一样

You have defined the Host model twice, I think that shouldn't have been the case. 您已经定义了两次主机模型,我认为不应该这样。 I am pretty new to ember and haven't used async:true feature, but I have been able to do things like(but I hadn't used nested route): 我是ember的新手,并没有使用async:true功能,但我能够做的事情(但我没有使用嵌套路由):

App.Host = DS.Model.extend({
    hostName: DS.attr('string')
    probes: DS.hasMany('probe')
});

App.Probe = DS.Model.extend({
    probeName: DS.attr('string')
    checks: DS.hasMany('check')
});

App.Check = DS.Model.extend({
    checkName: DS.attr('string')
});

and you can spin up a rest api for host that returns : 并且您可以为返回的主机启动一个rest api:

{
    "hosts": [
        {
            "hostName": "SomeHostName1",
            "id": "SomeHostId1",
            "probes":["p1","p2"]
        },
        {
            "hostName": "SomeHostName2",
            "id": "SomeHostId2",
            "probes":["p2","p3"]
        }
    ],
    "probes": [
        {
            "probeName": "SomeProbeName1",
            "id": "p1",
            "checks":["c1","c2"]
        },
        {
            "probeName": "SomeProbeName2",
            "id": "p2",
            "checks":["c2","c3"]
        }
    ],
    "checks": [
        {
            "checkName": "SomeCheckName1",
            "id": "c1"
        },
        {
            "checkName": "SomeCheckName2",
            "id": "c2"
        }
    ]
} 

In my case I didn't have nested route but I think we should be able to set the controller content from the master payload somehow since all the required content are in store already! 在我的情况下,我没有嵌套路由,但我认为我们应该能够以某种方式从主有效负载设置控制器内容,因为所有必需的内容已经存储! I don't know if it was of any help, but this is something I would also like to know the answer of. 我不知道它是否有任何帮助,但这也是我想知道的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM