Skip to content

Cached computed properties seem to be invalidated too often #10433

@qbolec

Description

@qbolec

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'));

http://jsfiddle.net/L95npwyj/

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions