adopted-ember-addons / ember-changeset

Ember.js flavored changesets, inspired by Ecto
http://bit.ly/ember-changeset-demo
MIT License
431 stars 141 forks source link

Tracked mirror approach for prop notifications #591

Open betocantu93 opened 3 years ago

betocantu93 commented 3 years ago

@snewcomer This is another approach for #586, property notifications

We use tracked-built-ins to have a common tracked object reference that we can keep notifying of changes by lazily consuming tags on every changeset.get.

This PR also implements addObserver and removeObserver to allow client code to add / remove observers in an ergonomic way to a changeset.

The only "downside" AFAIK is that for classic computeds, we have to manually subscribe to the changeset.mirror property. One way to solve this, is to just use native getters or, create a native getter which consumes the value and then add that native getter to the dependencies of your computed, i.e.

get myProp() {
  return this.args.changeset.get(this.args.path);
}

@computed('myProp')
get someComputed() {
 return ....
}

//or
@computed(`args.changeset.mirror.yourpath`)
...

Here's the sandbox im using for this DEMO: https://github.com/betocantu93/changeset-tests/tree/try-mirror

Notice: please use try-mirror branch