2017-01-30 40 views
0

主記錄,應用程序/模型/賬號:如何使用Ember Data加載嵌套記錄(主 - 明細記錄)?

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    username: DS.attr('string'), 
    emailaddress: DS.hasMany('emailaddresses'), 
    birthdate: DS.attr('date'), 
    gender: DS.attr('boolean') 
}); 

的詳細記錄,應用程序/模型/ EMAILADDRESS:

import DS from 'ember-data'; 

export default DS.Model.extend({ 
    account: DS.belongsTo('account'), 
    emailaddress: DS.attr('string'), 
    verificationcode: DS.attr('string'), 
    isverified: DS.attr('string') 
}); 

來自服務器的虛擬JSON字符串:

{「id」:0,「username」:「ikevin」,「birthdate」:「Jan 30,2017 1:34:38 PM」,「gender」:true,「emailaddresses」:[{「id」:0 「EMAILADDRESS」: 「[email protected]」, 「verificationcode」: 「AAAAAA」, 「isverified」:發LSE}]}

適配器/app/adapters/account.js

import ApplicationAdapter from './application'; 

export default ApplicationAdapter.extend({ 

    urlForQueryRecord(query) { 
    if (query.profile) { 
     delete query.profile; 
     return `${this._super(...arguments)}/profile`; 
    } 

    return this._super(...arguments); 
    } 

}); 

路由應用程序/路由/ index.js:

import Ember from 'ember'; 
import RSVP from 'rsvp'; 

export default Ember.Route.extend({ 
    model() { 
    return RSVP.hash({ 
     walletbalance: this.get('store').queryRecord('wallet', {balance: true}), 
     instagramfriendscount: this.get('store').queryRecord('instagram', {friendscount: true}), 
     accountprofile: this.get('store').queryRecord('account', {profile: true}) 
    }); 
    } 
}); 

和應用程序/模板/ components/account-profile.hbs:

<div class="box"> 
    <div class="title">Your Profile</div> 
    <div>Username: {{account.accountprofile.username}}</div> 
    <div>Email Address: {{account.accountprofile.emailaddess}}</div> 
    <div>Birthdate: {{account.accountprofile.birthdate}}</div> 
    <div>Gender: {{account.accountprofile.gender}}</div> 
</div> 

我認爲這裏有2個問題:

  1. 在Chrome Ember插件中,模型類型「emailaddress」的數據始終爲0.所以,這意味着它不會被加載。

  2. 在app/templates/components/account-profile.hbs中,{{account.accountprofile.emailaddess}}未指向正確的字段。注意:目前預計只顯示1個電子郵件地址。

如何解決這些問題以加載和顯示嵌套記錄?

謝謝!

回答

0

是的,我解決我自己:

我把它改爲嵌套的數組(按指定位置:http://thejsguy.com/2016/01/29/working-with-nested-data-in-ember-data-models.html

所以從服務器返回的字符串變成:

{「ID 「:0,」username「:」ikevin「,」birthdate「:」2017年1月30日2:01:14 PM「,」gender「:true,」emailaddresses「:[{」emailaddress「:」aaa @ bbb .com「,」verificationcode「:」AAAAAA「,」isverified「:false}]}

而在.hbs:

<div>Email Address: {{account.accountprofile.emailaddresses.0.emailaddress}}</div> 

它顯示的電子郵件地址!

謝謝!

+0

我也刪除了之前生成的「emailaddress」模型。 – ikevin8me