0

I am trying to return a array promise to my model so that I can loop through the results in my template. I need the contact products to return as an promise array and not a promise object.

Model :

App.ContactEditorRoute = Ember.Route.extend({
   model: function (params) {
    return Ember.RSVP.hash({
        contact: this.store.find('contact', params.id),
    /*need this to return array not an object which is currently does*/
        contactproducts: this.store.find('contactproduct',params.id),
        products: this.store.find('product') 
    })
}

Template: (using emblem for markup)

each contactproduct in contactproducts
      p
        ' quantity
        contactproduct.quantity
        ' notes
        contactproduct.note
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
Kyle Snell
  • 81
  • 1
  • 1
  • 11
  • 1
    You're searching by id, it doesn't return a collection when you specify a resource id. Why does it need to be a collection? – Kingpin2k Jun 13 '15 at 19:57
  • I need to be able to loop through the results. Each contact has 0 to many products stored in contact products – Kyle Snell Jun 13 '15 at 20:09
  • 1
    this might help you: http://guides.emberjs.com/v1.12.0/models/finding-records/ – artych Jun 13 '15 at 20:33
  • if contact products is a hasMany on the contacts, why are you fetching it in the rsvp hash? It should be a part of the contact model... – Kingpin2k Jun 13 '15 at 20:35

1 Answers1

1

If you really want it to be an array with the way you have your code currently structured, then you can do

contactproducts: this.store.find('contactproduct',params.id)
    .then(product => [product])

This is an approach to take when you want to do some additional processing on the result of a find, yet still return a promise as model is designed to do.

However, as another poster indicated this is probably not what you want to be doing. If contact products are a hasMany property of contacts, then they will already be there (or perhaps fetched for you based on {async: true}).

If contacts has no hasMany('contactproduct'), then you may need to fetch them yourself, but I doubt if contact products have the same id as contacts. If they do, they shouldn't. model IDs should be unique. Assuming contact products have a belongsTo pointing back to contact then you want to do this:

contactproducts: this.store.find('contactproduct', { contact: params.id })

which will return a (promise for) an array.

However, in general in such cases it's better to put the additional retrieval in the afterModel hook. That allows you keep your model nice and clean.

  • Thank you. I was looking for the syntax with `contactproducts: this.store.find('contactproduct', { contact: params.id })` . The data design has contactproducts pulled out into a separate table because there is a quantity attribute for each contact to product relationship. – Kyle Snell Jun 14 '15 at 17:42
  • @KyleSnell side-note, you seem to be using a proxying controller (ObjectController) since you refer to `contactproducts` in the template and not `model.contactproducts`. this is deprecated behaviour and the explicit version is more maintainable. – locks Jun 15 '15 at 12:22