Knowing when binding triggered on a property


#1

Hi all,

Do you see any way to know when ANY model’s property has been modified through a binding?
I would need something generic because it would be applied to all the forms of the application. This means I cannot just have a 'property’Changed() observable callback for every properties of the models. I’m thinking along the ways of overriding the properties setters created by the binding engine so they can call a single defined callback but I feel like there could be a better way.

Thanks!


#2

From your wording, I’m not sure if you have tried propertyChanged(name, newValue, oldValue) yet? It’s still not global, but it at least can be applied to any @bindable applied property. Then from that change handler, you can notify globally

export class Bla {
  @bindable name
  @bindable addresss
  @bindable fone

  @bindabe dontCare

  propertyChanged(name, newValue, oldValue) {
    if (name === 'dontCare') {
      return;
    }
    // it's the form input change?
  }
}

#3

Interesting! I didn’t know about this propertyChanged handler! Although I’m not sure I could apply this to my case as is because my models won’t always have observable properties. I’ll dig in this direction through aurelia’s code, anyway : this seems quite close to what I would like and I might be able to apply this behavior to my use case.
Thanks!


#4

Ok, I ended up just using the binding engine to watch all properties changes. This allowed me to implement my isDirty checks without modifying the existing models…

So the final code looks like this:

Object.getOwnPropertyNames(obj).forEach(p => {
        this.subscriptions.push(this.binding.propertyObserver(obj, p)
           .subscribe(() => this.updateDirty()));
    });

my updateDirty() method is called after every property change and no change was necessary to the model.

If anyone can come up with a better solution (because I’m not totally sure of the trade-off between the previous dirty checking and the current overhead of the observers), I’m still interested but this fits my needs for the time being