canjs / can-observe

Observable objects
https://canjs.com/doc/can-observe.html
MIT License
20 stars 2 forks source link

Implement getKeyDependencies for computed getters #37

Closed m-mujica closed 6 years ago

m-mujica commented 6 years ago
class Person extends ObserveObject {
  get fullName() {
    return this.first + " " + this.last;
  }
}

var me = new Person();
me.first = "John";
me.last = "Doe";

draw(getGraph(me, "fullName"));

screen shot 2017-12-29 at 10 21 50 am

m-mujica commented 6 years ago

@justinbmeyer the dependency graph of the getter in an array is kinda interesting (this is from the over21 getter example):

screen shot 2017-12-29 at 10 30 53 am

justinbmeyer commented 6 years ago

over21 makes sense to me.

justinbmeyer commented 6 years ago

though should it be Person{}.age 6? Is the constructor named?

m-mujica commented 6 years ago

@justinbmeyer The code I used to render that graph is

var People = ObserveArray.extend(
  "People",
  {},
  {
    get over21() {
      return this.filter(function(person) {
        return person.age > 21;
      });
    }
  }
);

var people = new People([
  { id: 1, age: 22 },
  { id: 2, age: 21 },
  { id: 3, age: 23 }
]);

That's why there are no Person instances in the graph; if I wrap the objects in the People array with a Person class I do get the right labels:

screen shot 2018-01-08 at 10 45 26 am

I think it's all fine, let me know if you disagree.

I'm going to merge and release 💥