你如何访问Emberjs中的嵌套对象?

时间:2017-10-19 04:12:29

标签: ruby-on-rails ember.js ember-data

我正在尝试从ember模型访问嵌套对象。 模型看起来像这样: 含量:

export default DS.Model.extend({
  title: DS.attr('string'),
  text: DS.attr('string'),
  createdBy: DS.attr('string'),
  status: DS.attr('string'),
  contentType: DS.attr('string'),
  author: DS.belongsTo('author', { embedded: true }),
  campaign: DS.belongsTo('campaign', { embedded: true })
});

作者:

export default DS.Model.extend({
  name: DS.attr('string')
});

广告活动:

export default DS.Model.extend({
  name: DS.attr('string')
});

我正在调用Rails REST API中的对象列表。数据如下所示:

 {
  "data": [
    {
      "id": "1",
      "type": "contents",
      "attributes": {
        "title": "The Great Escape",
        "text": "This is the way the world ends!",
        "status": "Draft",
        "content-type": "Blog Post",
        "created-by": "#\\u003cUser:0x007fe4920d8850\\u003e",
        "author": {
          "id": 3,
          "name": "Daniel Duck",
          "created_at": "2017-10-17T21:56:00.105Z",
          "updated_at": "2017-10-17T21:56:00.105Z"
        },
        "campaign": {
          "id": 3,
          "name": "Apple - Fall",
          "created_at": "2017-10-17T21:56:00.093Z",
          "updated_at": "2017-10-17T21:56:00.093Z"
        }
      }
    },
    {
      "id": "2",
      "type": "contents",
      "attributes": {
        "title": "Just a lonely Joe in search of his soul",
        "text": "One day he'll be a real boy.",
        "status": "Final",
        "content-type": "Email",
        "created-by": "#\\u003cUser:0x007fe4920d8850\\u003e",
        "author": {
          "id": 2,
          "name": "Roberta Rock",
          "created_at": "2017-10-17T21:56:00.103Z",
          "updated_at": "2017-10-17T21:56:00.103Z"
        },
        "campaign": {
          "id": 2,
          "name": "Blade Runner 2049",
          "created_at": "2017-10-17T21:56:00.091Z",
          "updated_at": "2017-10-17T21:56:00.091Z"
        }
      }
    },
    {
      "id": "3",
      "type": "contents",
      "attributes": {
        "title": "Love in the time of Silicon Valley",
        "text": "Maybe love IS all we need.",
        "status": "Waiting for Review",
        "content-type": "PR Release",
        "created-by": "#\\u003cUser:0x007fe4920d8850\\u003e",
        "author": {
          "id": 1,
          "name": "James Jackson",
          "created_at": "2017-10-17T21:56:00.101Z",
          "updated_at": "2017-10-17T21:56:00.101Z"
        },
        "campaign": {
          "id": 1,
          "name": "PRP",
          "created_at": "2017-10-17T21:56:00.089Z",
          "updated_at": "2017-10-17T21:56:00.089Z"
        }
      }
    }
  ]
}

我设置了内容路线:

export default Route.extend({
    model() {
        return this.get('store').findAll('content', {include: 'author'});
    }
});

我设置了模板:

<div class="jumbo">
<h1>Contents</h1>

</div>
{{#each model as |content|}}
    {{content-listing content=content}}
{{/each}}

我设置了这样的列表组件:

{{yield}}
<article class="content">
  <h3>{{content.title}}</h3>
  <div class="detail owner">
    <span>Author:</span> {{content.author}}
  </div>
  <div class="detail text">
    <span>Text:</span> {{content.text}}
  </div>
  <div class="detail status">
    <span>Status:</span> {{content.status}}
  </div>
  <div class="detail content_type">
    <span>Content Type:</span> {{content.contentType}}
  </div>
</article>

当我尝试列出对象时:

Contents

The Great Escape

Author: <DS.PromiseObject:ember389>
Text: This is the way the world ends!
Status: Draft
Content Type: Blog Post
Just a lonely Joe in search of his soul

Author: <DS.PromiseObject:ember392>
Text: One day he'll be a real boy.
Status: Final
Content Type: Email
Love in the time of Silicon Valley

Author: <DS.PromiseObject:ember395>
Text: Maybe love IS all we need.
Status: Waiting for Review
Content Type: PR Release

如何解析Promise并获取作者对象?

1 个答案:

答案 0 :(得分:0)

查看回复,content.author是一个对象

    {
      "id": 1,
      "name": "James Jackson",
      "created_at": "2017-10-17T21:56:00.101Z",
      "updated_at": "2017-10-17T21:56:00.101Z"
    }

尝试将hbs更改为content.author.name