【问题标题】:How to render hasMany relationship data?如何渲染hasMany关系数据?
【发布时间】:2013-03-25 03:04:50
【问题描述】:

如何获取以下示例代码,以便在呈现 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'
}];

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>

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    Ember-data 在这方面不像 ActiveRecord:当您使用 has_many 时,您必须拥有一个属性,该属性是 ids 的数组用于记录。在互惠模型中,拥有所有者的外键是不够的。

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

    如果您使用的是 RESTAdapter,这些键名将改为 phone_number_ids。如果您使用active_model_serializers,您可以使用embed 功能自动包含一个id 数组。

    【讨论】:

    • 将 Fixtures 更改为以下代码对 #/persons/1 App.Person.FIXTURES = [{ id: 1, firstName: 'X', lastName: 'Smith' 没有影响, phone_number_ids: [1, 2] }, { id: 2, firstName: 'Y', lastName: 'Smith', phone_number_ids: [3] }]; App.PhoneNumber.FIXTURES = [{ id: 1, person_id: 1, number: '20' }, { id: 2, person_id: 1, number: '23' }, { id: 3, person_id: 2, number: '42' }];
    • 啊,好吧,FixtureSerializer 的工作方式略有不同。将它们更改为phoneNumbers: [1, 2] 等。
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 2011-03-17
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2015-11-17
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多