2017-10-19 5 views
0

Emberモデルからネストされたオブジェクトにアクセスしようとしています。 内容::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" 
     } 
     } 
    } 
    ] 
} 

Iセットアップ内容ルート:データは次のようになり

:私はこのようなリストのコンポーネントを設定

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

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

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

私はテンプレートを設定しました

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

プロミスを解決して作成者オブジェクトを取得するにはどうすればよいですか?

+0

するhbsを変更してみてくださいオブジェクトであり、応答を見て、私は「その著者が下であるべきだと思います「関係」キーは「属性」の下にありません。だからそれは神秘的なデータを混乱させるかもしれない。 –

答えて

0

content.authorは、私はあなたの応答が正しくフォーマットされていないことを仮定してい

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

content.author.name

+0

私はそれを試みた。それはエラーを投げるのに使用されていました。今は何も表示されません。何とかその約束は解決する必要があり、それをどうやって行うのか分かりません。 :/ – Erick

関連する問題