2017-10-19 74 views
0

我想从一个烬模型访问一个嵌套的对象。 这些模型是这样的: 内容:如何访问Emberjs中的嵌套对象?

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> 

当我尝试列出objects:

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并获取作者对象?

+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" 
    } 

尝试改变hbscontent.author.name

+0

我试过了。它曾经抛出一个错误。现在它不显示任何东西。不知何故,承诺需要解决,只是不知道该怎么做。 :/ – Erick