Ember数据嵌套模型 [英] Ember Data nested Models

查看:143
本文介绍了Ember数据嵌套模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NDB的Google App Engine项目中使用EmberJs和Ember-Data。在数据库中我有Host,Probe和Check实体。只要我有我的REST api顺序,数据库模型并不重要,但为了清楚,这里是我的数据库类:

I'm using EmberJs and Ember-Data in a Google App Engine project which uses NDB. In the database I have Host, Probe and Check entities. 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:

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.


  • 主机

    • 探针

      • 检查

      在我的App.Js中,我定义了以下模型:

      In my App.Js I have defined the following models:

      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(){
      
                              });
                          });
                      });
                  });
              });
          });
      });
      

      如果已经构建了以下URL路径,则在AppEngine中:

      And in AppEngine if have built the following URL paths:

      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 返回:

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

      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 返回:

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

      我的模板是:

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

      现在我有这些...但不知道如何把它绑在一起,以便Ember-数据提供正确的http请求。到目前为止,我只看到请求去 http://example.com/modelName/

      Now I have all this... but no clue how to tie it up together so that Ember-Data makes the right http requests. So far I've only seen request go to http://example.com/modelName/

      推荐答案

      目前,Ember Data不支持API端点的这种类型的嵌套路由。有一些谈论这个,但似乎没有取得任何进展。

      Currently Ember Data does not support this type of nested routes for API endpoints. There's been some talk about this, but it doesn't seem to be making any forward progress.

      这篇关于Ember数据嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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