Wednesday, December 30, 2015

Aurelia - handling validation of bound properties in a custom element

As I have mentioned before, much of my application consists of custom elements. This is one of the major tools that Aurelia provides for managing complexity. However, there is a trade-off, and it comes from the asynchronous nature of the framework. Once I move some of my UI and code into a custom element, the relationship between the parent and the child becomes asynchronous, managed through the data binding process. This means that the custom element does not have access to the data in the parent in its constructor, but only once the attached() method is called.

I have a couple of custom elements with their own validation, and in some cases, I want the message for the validation to be dependent on properties that are data bound. If I set the validation up in the constructor, I won't get the correct message.

So I set it up in attached(). It's really that simple. Some of my validation is in the constructor, and some in attached().

No comments: