I think I have found a bug on computed property. I have a component my-cell, responsible for render 1 event.
Please take a look at my component/my-cell.js
export default Ember.Component.extend({
isHighlighted: Ember.computed('highlight', 'event.createdAt', function() {
//set highlight to true if event.createdAt has the same time as highlight time
}
});
When isHighlighted is called, I put a breakpoint there, I found that this.get('event.createdAt') returns undefined.
To be more informative, event is an object from data store. So this.get('event') would get me the event, but all properties of event are undefined.
When the page is finished loading, by Ember Inspector, event.createdAt is not undefined anymore.
I suspect my problem is because Ember will not load the whole object with its property into component.
How do I get around this? Is my approach right or not? Please point me out if I made any mistakes. Thank you.
I don’t consider this a bug. When you declare event.createdAt as a dependent of your computed property, Ember will set up two observers, one for the event binding itself, another for the createdAt value of event.
What I’m guessing happens is that since event is an Ember Data record it might not be fully resolved by the time the computed property runs the first time.
I suggest using getWithDefault or checking if createdAt and returning false.
Something’s worrying me though:
This makes it both sound like you’re using a computed property as an observer, and that you’ll create an endless look, because you’re setting highlight from inside the computed property, which will trigger the computed property, which will make it set highlight which triggers the compu— you get my drift
If the above is a correct assessment I suggest hooking into didInsertElement instead:
Using a hook to do this task is a much cleaner solution. However didInsertElement hook is not the correct one. When this hook is called, this.get('event.createdAt') returns undefined.
The hook didReceiveAttrs works for me. This hook is called twice. The first time it called, this.get('event.createdAt') returns undefined, but when it called second time, I get my this.get('event.createdAt').
I also tried the hook didRender. It also works, and it is called 3 times. However I get the following warning:
DEPRECATION: A property of X was modified inside the didUpdate hook. You should never change properties on components, services or models during didUpdate because it causes significant performance degradation. [deprecation id: ember-views.dispatching-modify-property]
It sounds weird to me because I did not call any didUpdate hook in my code. Maybe Ember is messing up didRender with didUpdate. But why changing properties on components, services or models during didUpdate with cause significant performance degradation?