Aamir Mahmood
Aamir Mahmood

Reputation: 2724

Ember hasMany on a single route

I am working on Employee and employee can have multiple addresses, When added/editing an employee, addresses are on the same route/page.

On the page I have Add Address button, when that is clicked it should add address fields' set

here is some code

addAddress: function () {

  const addressNew = this.store.createRecord('employee/address', {});
  ////addressNew.set('id', 999);

  const employee = this.controller.get('model');
  employee.get('address').pushObject(addressNew);
},

When I press add address button, it gives me error

Uncaught Error: You must provide a string key when calling `yieldItem`; you provided null

When I uncomment the ID line, it adds one address, but when I click 2nd time it give me duplicate ID message.

What should be the proper way to handle hasMany relation on the single page

Models

// employee 
import DS from 'ember-data';

export default DS.Model.extend({
  username: DS.attr('string'),
  password: DS.attr('string'),
  fname: DS.attr('string'),
  lname: DS.attr('string'),
  email: DS.attr('string'),
  department: DS.attr('string'),
  hire_date: DS.attr('date'),
  status: DS.attr('boolean'),
  gmail: DS.attr('string'),
  timezone: DS.attr('string'),
  address: DS.hasMany('employee/address')
});


// employee/address
import DS from 'ember-data';

export default DS.Model.extend({
  address_1: DS.attr('string'),
  address_2: DS.attr('string'),
  phone: DS.attr('string'),
  cell: DS.attr('string'),
  city: DS.attr('string'),
  state: DS.attr('string'),
  country: DS.attr('string'),
  default_address: DS.attr('boolean'),
  employee_id: DS.belongsTo('employee')
});

I am using

DEBUG: Ember             : 1.13.7
DEBUG: Ember Data        : 1.13.8
DEBUG: jQuery            : 2.1.4
DEBUG: Ember Simple Auth : 0.8.0

Edit

I think it is something with my template that it is not allowing me to add and give me error I am using

{{#each model.address key="id" as |address|}}

to loop, so it dont have ID and it give yield error. I changed key="@guid" and it worked. I need a unique index as well, and @guid or @index are not working ...

Upvotes: 2

Views: 101

Answers (1)

Aamir Mahmood
Aamir Mahmood

Reputation: 2724

As @Gennady Dogaev suggested, I changed it. My issue was with the template

I was using

{{#each model.address key="id" as |address|}}

I have changed that to

{{#each model.address as |address index|}}

now it is working fine as expected

Upvotes: 1

Related Questions