How should the property change with count of associated records?


#1

Hi,

I am creating my first ember app using ember-cli and now facing issue with setting new property at controller.

I have model associations as follows:

Offer has many Items and item belongs to Offer.

Now i want find the offer without items and return that offer-id.

Offers.index controller:

export default Ember.ArrayController.extend({

  pendingOffer: function () {
    var pending_offer = this.findBy('items.length',0);
    var offer_id = pending_offer ? pending_offer.id : null
    return offer_id;
  }.property('offers.@each.items.length')

});

But here when i add item to an existing offer, it still returns the old offer, it works proper on page-refresh, but property is not updating…

there might be some issue with property('offers.@each.items.length') Can anyone please help me to make it work?

Thanks :smile:


#2

Finally able to get it done…

Offer model:

import DS from 'ember-data';
var attr = DS.attr,
    hasMany = DS.hasMany;

export default DS.Model.extend({
      // other attributes

      itemCount: function() {
        return this._data.items.length;
      }.property('this.items.@each')
    });

Offers.index controller

import Ember from 'ember';

export default Ember.ArrayController.extend({

  pendingOffer: function () {
    var pending_offer = this.findBy('items.length',0);
    var offer_id = pending_offer ? pending_offer.id : null;
    return offer_id;
  }.property("content.@each.itemCount")

});

And its done…!! Now whenever I add item to an offer it updates pendingOffer value.