-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Consider this code:
var UserController = Ember.ObjectController.extend({
firstName: "John",
lastName: "Doe",
largeFirstLetter:function(){
console.log("Recomputing");
return this.get('firstLetter').toUpperCase();
}.property('firstLetter'),
firstLetter:function(){
return this.get('fullName').substr(0,1);
}.property('fullName'),
fullName:function(){
return this.get('fisrtName') + ' ' + this.get('lastName');
}.property('firstName','lastName')
});
var userController = new UserController();
console.log(userController.get('largeFirstLetter'));
console.log(userController.get('largeFirstLetter'));
userController.set('lastName','Smith');
console.log(userController.get('largeFirstLetter'));
The idea is that we have a dependency tree :
largeFirstLetter <- firstLetter <- fullName <-- firstName
<-- lastName
And the operations we perform change lastName, and fullName, but not firstLetter.
The output on the console is:
Recomputing
U
U
Recomputing
U
Ember spec seems to promise to me that largeFirstLetter will be recomputed only if it's dependencies are no longer valid. But the experiment reveals that what it actually means is that "deeper" dependencies, such as lastName are also taken into account. So even though firstLetter (the only direct dependency of largeFirstLetter) does not change, the largeFirstLetter gets recomputed.
The example above is very simple, but we hit this problem in a more advanced scenario where it was causing a lot of redraws in situations where nothing actually changed.
I've "fixed" it with making firstLetter a real (not computed) property, and manually recomputing it using observe('fullName').
Am I doing something wrong?