Domanda

I have defined two models that each have a Many-to-Many relationship. I want to show a certain amount of 'people' to be a part a 'department'. How would I insert more people into a department? When I try to insert a 'person' into a 'department', the 'department' does not recognize the person's name as being a part of the 'person' model.

I've stated the relationship in the model

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')
});

And the controllers

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({});

I won't post the HTML (.hbs) templates because they are incorrect.

È stato utile?

Soluzione

 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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top