Emberjs模型协会。调用具有许多其他模型的模型

时间:2013-12-11 15:02:13

标签: ember.js model relationship yeoman model-associations

我定义了两个模型,每个模型都有多对多关系。我想展示一定数量的“人”成为“部门”的一部分。我如何在部门中插入更多人?当我尝试将“人”插入“部门”时,“部门”不会将该人的姓名识别为“人”模型的一部分。

我已经在模型中说明了这种关系

VpcYeoman.Department = DS.Model.extend({
  departmentName: DS.attr('string'),
  departmentMembers: DS.hasMany('person')
});

&安培;

VpcYeoman.Person = DS.Model.extend({
  profileName: DS.attr('string'),
  profileDepartment: DS.hasMany('department')
});

控制器

VpcYeoman.PeopleController = Ember.ObjectController.extend({
    actions: {
    createPerson: function () {
      // Get the todo title set by the "New Todo" text field
      var profileName = this.get('profileName');
      if (!profileName.trim()) { return; }

      // Create the new Todo model
      var person = this.store.createRecord('person', {
        profileName: profileName,
        isCompleted: false
      });

      // Clear the "New Todo" text field
      this.set('profileName', '');

      // Save the new model
      todo.save();
    }
  }
});

VpcYeoman.DepartmentsController = Ember.ArrayController.extend({});

我不会发布HTML(.hbs)模板,因为它们不正确。

1 个答案:

答案 0 :(得分:1)

 var person = this.store.createRecord('person', {
        profileName: 'joe shmo'
      });

 var dept = this.store.createRecord('department', {
        departmentName: 'admin'
      });

dept.get('departmentMembers').pushObject(person);

person.get('profileDepartment').pushObject(dept);