Rigel
Rigel

Reputation: 882

Updated : FirstObject is undefined emberjs array

I need to get firstObject of my carousel and set it as active this is how I am setting carousel property

JSBIN for code

App.ApplicationController = Ember.ArrayController.extend({
    content: [],
    carouselData: function () {
        var categories = this.get('content');
        var products = Em.A([]);
        categories.forEach(function (category) {
            category.get('items').then(function (data) {
                data.forEach(function (product) {
                    products.addObject(product);
                });
            });
        });
        console.log(products);
        console.log(products.get('firstObject'));
        return products;
    }.property('content')
});

Update

@ppcano Thanks for the explanation :). I got what you are asking me to do. return model only when hasMany has fulfilled. and then with computed property save them in carouselData property. but may be I am missing something in implementation the cdata.get('firstObject') returns a promise updated jsbin UPDATED JSBIN in App.Caroure

update 2 SOLVED enter link description here

Upvotes: 0

Views: 1049

Answers (1)

ppcano
ppcano

Reputation: 2861

Your problem is that the computed property does not work within async execution.

category.get('items').then(function (data)

The products variable is returned before any data can be pushed into products, because the items must be requested.

You could solve it when you ensure that items are loaded when the property is computed. You could do it in your route model as:

model: function(){

    return this.store.find('facture').then(function(factures){

      var productPromises = factures.getEach('items');

      return Ember.RSVP.all(productPromises).then(function(products) {
         return factures;
      }); 

    });

}

Then, you could define your CP as:

  carouselData: function(){

    var result = Em.A([]);

    this.get('content').getEach('items').forEach(function(items){
      result.pushObjects(items.toArray());
    });

   return result;

 }.property('[email protected].@each')

Upvotes: 3

Related Questions