Monday, September 10, 2012

Observable properties pattern

If you’re familiar with C#, think about the INotifyPropertyChanging and INotifyPropertyChanged interface to implement observable properties.

Here are some points before we implement observable properties in JavaScript:

- Properties  implemented as methods
- Check the incoming value and decide if you want to update
- Return private variable
- Store event handlers in an array
- Utilise return values to abort the updating process

Here is the implementation:

Note: With ECMAScript5 properties can have methods body (similar to how .NET properties look) and it's very powerful, but be careful because this option is available in current generation browsers.

Here is an example:

No comments:

Post a Comment