【问题标题】:Emberjs Model Associations. Calling a model that hasMany of another modelEmberjs 模型协会。调用具有许多其他模型的模型
【发布时间】:2013-12-11 15:02:12
【问题描述】:

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

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

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) 模板,因为它们不正确。

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2023-03-30
      • 2016-12-31
      相关资源
      最近更新 更多