【问题标题】:bind relational data to view in ember.js绑定关系数据以在 ember.js 中查看
【发布时间】:2013-11-24 20:39:03
【问题描述】:

我有两个模型:CompanyPerson。一个公司有很多人,一个人属于一个公司。我试图在 UI 中表示这一点。不过,我很难弄清楚如何将关系数据绑定到视图。

这是我的 Person 模型

App.Person = DS.Model.extend({

  firstName: DS.attr('string'),

  lastName: DS.attr('string'),

  city: DS.attr('string'),

  state: DS.attr('string'),

  email: DS.attr('string'),

  company: DS.belongsTo('company'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName')
});

这是我的公司模型

App.Company = DS.Model.extend({

  name: DS.attr('string'),

  people: DS.hasMany('person')

});

这是我的路线

App.Router.map(function() {

    this.resource('people', function() {

        this.resource('person', { path: ':person_id'});

        this.route('new');
    });
});

App.PersonRoute = Ember.Route.extend({

    model: function(params) {

        return this.store.find('person', params.person_id);
    }
});

这是我的看法

<script type="text/x-handlebars" data-template-name='show/_edit'>
<div>
  <form role="form">
  <div class="form-group">
    <label for="firstName">First Name</label>
    {{input type="text" value=firstName class="form-control" id="firstName" placeholder="First Name"}}
  </div>
  <div class="form-group">
    <label for="lastName">Last Name</label>
    {{input type="text" value=lastName class="form-control" id="lastName" placeholder="Last Name"}}
  </div>
  <div class="form-group">
    <label for="email">Email address</label>
    {{input type="email" value=email class="form-control" id="email" placeholder="Email"}}
  </div>
  <div class="form-group">
    <label for="city">City</label>
    {{input type="text" value=city class="form-control" id="city" placeholder="City"}}
  </div>
  <div class="form-group">
    <label for="state">State</label>
    {{input type="text" value=state class="form-control" id="state" placeholder="state"}}
  </div>
  <div class="form-group">
    <label for="company">Company</label>
    <span>{{company.name}}</span>
  </div>
  <button type="submit" class="btn btn-default" {{action 'doneEditing'}}>Done</button>
</form>

</div>

但是,从来没有向companies/2 发出请求。我是否需要在路由器中返回个人及其公司的 RSVP 哈希以适应这种情况?

这是从/people/https://gist.github.com/anonymous/7632294返回的json

【问题讨论】:

  • 您的示例似乎正确,您能显示从服务器返回的 json 吗?
  • 当然,在我到达单一记录路线之前,我会得到一份人员名单。这是从该响应返回的 json gist.github.com/anonymous/7632294
  • 我回答了您的问题,我认为您的问题在于您的公司财产名称。如果您使用带有活动模型序列化程序的 Rails,则可以使用 DS.ActiveModelAdapter

标签: ember.js ember-data


【解决方案1】:

如果您使用的是RESTAdapter,则需要将company_id: "1" 更改为company: "1"

【讨论】:

    猜你喜欢
    • 2014-10-11
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多