如何渲染hasMany关系数据?

win*_*yer 11 ember.js ember-data

如何在呈现URL /#/ persons/1时,获取以下示例代码,不仅可以呈现人员数据,还可以呈现该人员的电话号码.我试着通过电话号码循环,{{#each phoneNumber in phoneNumbers}}但我必须有一个基本的理解错误.

现在我只得到:

在此输入图像描述

app.js

App = Ember.Application.create({
  LOG_TRANSITIONS: true,
  rootElement: '#container'
});

// Router
App.Router.map(function() {
  this.resource('persons', function() {
    this.resource('person', { path: ':person_id' });
  });
  this.resource('phoneNumbers', function() {
    this.resource('phoneNumber', { path: ':phone_number_id' });
  });
});

App.PersonsRoute = Ember.Route.extend({
  model: function() {
    return App.Person.find();
  }
});

App.PhoneNumbersRoute = Ember.Route.extend({
  model: function() {
    return App.PhoneNumber.find();
  }
});

// Models
App.Store = DS.Store.extend({
  revision: 11,
  adapter: 'DS.FixtureAdapter'
});

App.Person = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  phoneNumbers: DS.hasMany('App.PhoneNumber')
});

App.PhoneNumber = DS.Model.extend({
  number:  DS.attr('string'),
  person: DS.belongsTo('App.Person')
});

App.Person.FIXTURES = [{
  id: 1,
  firstName: 'X',
  lastName: 'Smith'
}, {
  id: 2,
  firstName: 'Y',
  lastName: 'Smith'
}];

App.PhoneNumber.FIXTURES = [{
  id: 1,
  person_id: 1,
  number: '20'
}, {
  id: 2,
  person_id: 1,
  number: '23'
}, {
  id: 3,
  person_id: 2,
  number: '42'
}];
Run Code Online (Sandbox Code Playgroud)

的index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Phone book</title>
  </head>
  <body>
    <div id='container'></div>

    <script type="text/x-handlebars" data-template-name="application">
      {{outlet}}
    </script>

    <script type="text/x-handlebars" data-template-name="persons">
      <p>All people:</p>
      <ul>
        {{#each controller}}
          <li>{{firstName}} {{lastName}}</li>
        {{/each}}
      </ul>
      {{outlet}}
    </script>

    <script type="text/x-handlebars" data-template-name="person">
      <h1>{{firstName}} {{lastName}}</h1>
      {{#if phoneNumbers.length}}
        <ul>
          {{#each phoneNumber in phoneNumbers}}
            <li>{{phoneNumber.number}}</li>
          {{/each}}
        </ul>
      {{/if}}
    </script>

    <script type="text/x-handlebars" data-template-name="phone_numbers">
      <ul>
        {{#each controller}}
          <li>{{number}}</li>
        {{/each}}
      </ul>
      {{outlet}}
    </script>

    <script type="text/x-handlebars" data-template-name="phone_number">
      <h1>{{number}}</h1>
    </script>    

  </body>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="js/handlebars.js"></script>
  <script type="text/javascript" src="js/ember.js"></script>
  <script type="text/javascript" src="js/ember-data.js"></script>
  <script type="text/javascript" src="js/app.js"></script>
</html>
Run Code Online (Sandbox Code Playgroud)

Chr*_*sey 8

在这方面,Ember-data不像ActiveRecord那样:当你使用时,has_many你必须有一个属性是id记录的s 数组.在互惠模型上为所有者提供外键是不够的.

尝试添加phoneNumbers: [1, 2]到您的第一个Person夹具,并添加到phoneNumbers: [3]第二个.

如果您使用的是RESTAdapter,则会phone_number_ids改为使用这些键名.如果使用,active_model_serializers您可以使用该embed功能自动包含一组ID.