0
votes

I have a model with hasMany relationship:

App.Offer = DS.Model.extend({
shop: DS.attr('number'),          //ext key
name: DS.attr('string'),
description: DS.attr('number'),

products: DS.hasMany('product', {embedded: true})
});

App.Product = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
});

The record are sideloaded by the server and if I use them in templates it works:

{{#each product in model.products}}
    <li>{{product.nome}} - {{product.descrizione}}</li>
{{/each}}

But what if I want load the products into an array in the controller?

actions: {
    var products = [];

    $.each(this.get('model').get('products'), function(index, value) {
        products[index] = value.get('nome');  //this doesn't work
    });
}
1

1 Answers

0
votes

You would do it like this:

actions: {
    var products = [];

    this.get('model').get('products').forEach(function(product) {
        products[index] = product;
    });
}

But you really shouldn't do it that way. There shouldn't be a need to create a new array. Just fetching this.get('model').get('products') should return an object with all the models and the ability to iterate or do whatever you like with it.