Reputation: 131
I have a service, and declare a property in the service
bucket: []
Later in the service, I add something to bucket.
I'm trying to set up in this same service an Ember computed property or observer to listen/respond to changes in bucket.
Something like:
bucketListener: Ember.computed('bucket', function() {
//do stuff in response to something being added to or removed from the bucket
}
But I can't get this to work. I've tried a lot of different permutations,using Ember.computed and Ember.observer, but I can never get bucketListener to fire.
I have checks in place and am sure that bucket is getting added to as expected or removed from as expected, but bucketListener still isn't getting called into.
Is there some trick to doing this within a service? Or am I just bungling something more basic?
Upvotes: 1
Views: 621
Reputation: 12872
To trigger computed properties, you need to use this.get('bucket').pushObject('something')
instead of usual push().
When you access/use bucketListener only then computed property will be called.
Working example twiddle.https://ember-twiddle.com/776ffb2be7e129655ef62e59b2dc8697?openFiles=services.cart.js%2C
Upvotes: 1
Reputation: 18692
Service is no different from usual Ember.Object in terms of computed properties or observer. Please use .[]
to watch for elements being added or removed and observer intead of CP to fire when property changes. You can also watch main property to be sure it will fire:
bucketListener: Ember.observer('bucket', 'bucket.[]', function() {
//do stuff in response to something being added to or removed from the bucket
}
Upvotes: 1