Model 从ember数据模型上的关系访问属性

Model 从ember数据模型上的关系访问属性,model,controller,ember.js,relationship,ember-data,Model,Controller,Ember.js,Relationship,Ember Data,我正在开发一个像社交网络这样的小应用程序来管理项目。现在,我正在网络上研究友谊关系。为此,我创建了一个名为“Person”的模型,具有以下属性: App.Person = DS.Model.extend ( { name: DS.attr('string'), place: DS.attr('string'), email: DS.attr('string'), password: DS.attr('string'), acade

我正在开发一个像社交网络这样的小应用程序来管理项目。现在,我正在网络上研究友谊关系。为此,我创建了一个名为“Person”的模型,具有以下属性:

App.Person = DS.Model.extend
(
   {
      name: DS.attr('string'),
      place: DS.attr('string'),
      email: DS.attr('string'),
      password: DS.attr('string'),
      academic: DS.attr('string'),
      professional: DS.hasMany('App.Work'),
      knowledge: DS.attr('string'),
      projects: DS.hasMany('App.Project'),
      friends: DS.hasMany('App.Person')
   }
);
现在,我正在尝试获取控制器中某个人的朋友列表,但我不知道如何访问此列表。尝试类似于
的操作。get('model')。get('friends')
在模板中显示类似
的字符串。什么是正确的访问方式?应该在控制器上,在路线上,我应该创建一个额外的视图或类似的东西吗

非常感谢你的帮助


编辑:

我已经添加了@ArturMalecki所说的内容,但是我得到了
。这就是我得到的:

个人控制器

App.PersonController = Ember.ObjectController.extend
(
  {
    friends: function() 
    {
      return this.get('model').get('friends');
    }.property('model.friends'),
  }
);
App.Router.map
(
  function()
  {
    this.resource('app', { path: '/' }, function ()
    {
      // Additional child routes
      this.route('friends');
      ...
    });
    this.resource('person');
  }
);

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

App.AppFriendsRoute = Ember.Route.extend
(
  {
    model: function()
    {
      return App.Person.find();
    }
  }
);
路由器

App.PersonController = Ember.ObjectController.extend
(
  {
    friends: function() 
    {
      return this.get('model').get('friends');
    }.property('model.friends'),
  }
);
App.Router.map
(
  function()
  {
    this.resource('app', { path: '/' }, function ()
    {
      // Additional child routes
      this.route('friends');
      ...
    });
    this.resource('person');
  }
);

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

App.AppFriendsRoute = Ember.Route.extend
(
  {
    model: function()
    {
      return App.Person.find();
    }
  }
);
把手模板

<script type="text/x-handlebars" data-template-name="app/friends">
  <h1>Friends</h1>
  <ul>
    {{#each model itemController="person"}}
      <li>{{name}}, from {{place}}. Him/her friends are <{{friends}}>
    {{else}}
      You've no still friends
    {{/each}}
  </ul>
</script>

首先,在路线中,您需要设置
模型
。假设您有
PersonRoute
PersonController

App.PersonRoute = Ember.Route.extend({
  model: function() {
    return App.Person.find()
  }
});
然后,您可以通过以下方式访问控制器中的
friends
属性:

App.PersonController = Ember.ObjectController.extend({
  someCustomMethod: function() {
    ...
    this.get('model').get('friends');
    ...
  },
})
请记住,您需要将人员添加到路线:

App.Router.map(function() {
 this.resource("person");
});
更新 如何使用视图中的多个关系的简单示例

HTML
我试过你的建议,但我得到了同样的结果@Artur。我已经更新了显示代码当前状态的原始问题。非常感谢您的帮助。嗨@DanielRodriguezMolina,我刚刚举了一个简单的例子,说明如何访问
DS.ManyArray
Excellent@ArturMalecki!我一直在尝试你的解决方案,效果非常好!非常感谢你,你救了我一周!只是一个小细节。。。你不认为把
friends:function{…}.property('model.friends')
放在属性的末尾会更好吗?在朋友发生变化时更新结果。再次感谢你们!不客气。您的
函数{…}.property('model.friends')
是正确的。我刚刚更新了exampel,并添加了访问
hasMany
关系的第二种方法。小提琴似乎没有生成任何输出。你能检查一下@ArturMałecki并发布更新吗?
App = Ember.Application.create({});

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

App.Person = DS.Model.extend({
    name: DS.attr('string'),
    friends: DS.hasMany('App.Person'),
});

App.Person.FIXTURES = [
    { id: 1, name: 'Person1', friends: [2,3] },
    { id: 2, name: 'FriendPerson1' },
    { id: 3, name: 'FreindPerson2' }

];

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

App.IndexController = Ember.ObjectController.extend({
    friends: function() {
        return this.get('model').get('friends');
    }.property('model.friends')
})