Computed property from controller to component doesn't work


#1

Here is my code on EmberTwiddle:

https://ember-twiddle.com/cc6b2ef203c68c272ad29f460aca5432

I’m trying to set a computed property in a component on a property that changes in a controller.

I pass the property from controller to component like this:

{{my-posts category=category}}

but the computedProperty on the component doesn’t work:

postsFiltered: Ember.computed('category', function () {
  console.log('computed postsFiltered() with category: ' + this.get('category'));
  this.set('myMessage', 'This is the message!');
}),

Why?


#2

computed properties are lazy, and do not compute until you use them… in your twiddle, you are not using the postsFiltered property, so it doesn’t get computed.

Use {{postsFiltered}} instead of using {{myMessage}} in the template. And change the postsFiltered computed property to return a value instead of setting another property…

postsFiltered: Ember.computed('category', function () {
  console.log('computed postsFiltered() with category: ' + this.get('category'));
  return this.get('category'); // <- return the category here (or return other computed value)
}),

then in the template:

Message on computed property "category": {{postsFiltered}}

Hope that helps


#3

Ok, thanks.

I have another question about computed properties:

https://discuss.emberjs.com/t/handle-two-or-more-properties-in-just-one-computed-property/12624