OneToOne与ember-model的关系

时间:2013-08-18 17:04:53

标签: ember-model

如何与ember-model实现一对一的关系?

我尝试了以下代码,但它不起作用。

App.Book = Ember.Model.extend({
  id: Ember.attr(),
  title: Ember.attr(),
  name: Ember.attr(),
  author: App.Author
});

App.Author = Ember.Model.extend({
  id: Ember.attr(),
  firstName: Ember.attr(),
  lastName: Ember.attr()
});

我还尝试了下一个代码但是当我尝试使用下一个代码设置作者时出现错误:

错误:您必须使用Ember.set()来访问此属性

var book = App.Book.create({
  author: App.Author.create({firstName: 'fred'})
});

App.Book = Ember.Model.extend({
      id: Ember.attr(),
      title: Ember.attr(),
      name: Ember.attr(),
      author: Ember.attr(App.Author)
    });

我正在使用RESTAdapter,我的JSON看起来像:

{title: 'booktitle', name: 'thename', author: {firstName: 'fred', lastName: 'last'}}

1 个答案:

答案 0 :(得分:2)

试试这个:

author: Ember.belongsTo('App.Author', {embedded: true})

如果不起作用,请尝试:

author: Ember.belongsTo('App.Author', { key: 'author', embedded: true})