2017-10-19 2 views
0

Ich versuche, von einem Ember-Modell auf ein verschachteltes Objekt zuzugreifen. Die Modelle sehen wie folgt aus: Inhalt:Wie greifen Sie auf verschachtelte Objekte in Emberjs zu?

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

Autor:

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

Kampagne:

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

Ich bin eine Liste von Objekten aus einer Rails-REST-API aufrufen. Die Daten sieht wie folgt aus:

{ 
    "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" 
     } 
     } 
    } 
    ] 
} 

ich Setup die Inhalte Route:

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

ich die Vorlage ein:

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

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

ich die Auflistung Komponente wie folgt aufgebaut:

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

Wenn ich versuche, die aufzulisten Objekte:

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 

Wie behebe ich die Promise und bekomme das Objekt des Autors?

+0

Ich gehe davon aus, dass Ihre Antwort ist nicht richtig formatiert, ich denke, dass Urheber unter sein sollte " Beziehungen "Schlüssel nicht unter" Attribute ". Das kann die Glutendaten verwirren. –

Antwort

0

an der Reaktion der Suche, content.author ist ein Objekt

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

Ändern Versuchen die hbs sein content.author.name

+0

Ich habe das versucht. Es hat einen Fehler verursacht. Jetzt zeigt es nichts mehr. Irgendwie muss dieses Versprechen gelöst werden, nur nicht sicher, wie es geht. : / – Erick

Verwandte Themen