1

so currently i have

App.Mail = DS.Model.extend({
  name: DS.attr('string'),
  place: DS.attr('string'),
  note: DS.attr('string')
});


App.Envelope = DS.Model.extend({
  color: DS.attr('string'),
  weight: DS.attr('string'),
  name: DS.attr('string'),
  mail: this.store.find('mail',{'name':this.get('name')})
});

I am looking for something of the above functionality so that in my template when i do

{{#each envelope in envelopes}}
  {{content.envelope.mail.note}}
{{/each}}

Then it will print the correct the note for each mail in that each loop?

Im not sure if i am approaching this right.. but i am not sure how to get this functionality. Thanks!b

user3554230
  • 283
  • 2
  • 11

2 Answers2

0

You would set the mail attribute as a hasMany relationship.

App.Envelope = DS.Model.extend({
  color: DS.attr('string'),
  weight: DS.attr('string'),
  name: DS.attr('string'),
  mail: DS.hasMany('mail', {async:true}) 
});

If you need the url to be /mails?name=personsname the easiest route would be to use links in the original envelope response

{
  envelopes: [
    {
      id: 1,
      name: 'foo',
      color: 'green',
      weight: '12oz',
      links: {
        mail: '/mails?name=foo'
      }
    }
  ]
}

If you don't want to go down that route you can create a custom adapter: How do you create a custom adapter for ember.js?

Your template would look something like this:

{{#each mail in mails}}
  {{mail.note}}
{{/each}}
Community
  • 1
  • 1
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
0

I think you would be better off leveraging the DS.belongsTo and DS.hasMany

App.Mail = DS.Model.extend({
    envelope: DS.belongsTo('envelope'),
    name: DS.attr('string'),
    place: DS.attr('string'),
    note: DS.attr('string')
});


App.Envelope = DS.Model.extend({
    mail: DS.hasMany('mail') // use DS.belongsTo('mail') if 1-1 relationship

    color: DS.attr('string'),
    weight: DS.attr('string'),
    name: DS.attr('string')

});

the belongsTo would be the id of the parent the child record belongs to. hasMany would be an array of child records the parent owns.

you will also want to either set a property called 'envelopes' in your controller, you've created a computed property, or access it as part of some other scope(ex. a computed alias)

then you can access the note via

//assumes 1-1 relationship
{{#each envelope in envelopes}}
    {{envelope.mail.note}}
{{/each}}

//of if an envelope has many mails
{{#each envelope in envelopes}}
    {{#each mail in envelope.mails}}
        {{envelope.mail.note}}
    {{/each}}
{{/each}}

some potentially useful links

belongsTo API spec

hasMany API spec

really great guide that helped me increase my understanding of ember-data

kaungst
  • 136
  • 6