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

查看:17
本文介绍了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')
      });
      

      我定义了以下路由器:

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

      如果在 AppEngine 中构建了以下 URL 路径:

      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-Data 发出正确的 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天全站免登陆