mardi 25 août 2015

Ember data and promises in model

I'm currently trying to load an array of promises to the model, but when I start looping through results in the template, I can't access any relationships. Even though if I just return a single promise, I can access the relationships within the template.

I'm I missing something on how to handle the return values from the promises to be handle as if it was single?

Model - grouped item

export default DS.Model.extend({
  group:          DS.belongsTo('group', { async :  false }),
});

Model - group

export default DS.Model.extend({
    code:          DS.attr('string'),
    name:          DS.attr('string'),
});

Single model example

model : function(){
   return this.store.query('groupedItem', { group_code: 'HOME', position : 1 });
}

In template I can access model relationships like this

{{#each model as |section|}}
  <p>{{section.id}}</p>
  <p>{{section.group.id}}
{{/each}}

------ Model as array promise --------

model : function(){
   return Ember.RSVP.Promise.all(Ember.A([
      this.store.query('groupedItem', { group_code: 'HOME', position : 1 }),
      this.store.query('groupedItem', { group_code: 'HOME', position : 2 }),
    ]))
}

In template, accessing relationships fails

{{#each model as |section|}}
  {{#each section.content as |item|}}
     <p>{{item.id}}</p>
     <p>{{item.group.id}}
  {{/each}}
{{/each}}




Aucun commentaire:

Enregistrer un commentaire