In my previous blog post I mentioned WinJS observables and their usage and pitfalls. There are several alternative implementations which could help us to overcome that issues. Let’s see first of them.
Just repeat what’s our goal in BDD style:
AS a developer
I WANT to be able to define observable properties on the class as well as other class parts: get/set properties and methods and I don’t want to pay any unnecessary performance penalties
SO THAT the observable object will be able to notify listeners that the property has changed
In this blog post we’ll look at the manual approach. We need to do two things:
- ensure that the class has all required methods and fields needed for being observable
- wrapping the property with the observable functionality There are mixins which define the required methods and fields.
So the first requirement can be solved by defining a base class with all mixins
The second requirement can be solved by defining the helper method:
So let’s put the things together:
Deriving from ObservableBase class ensures that new class has all mixins imported and observableProperty helper function creates property definition with get/set wrapper methods. The most important is the “set” method. This method makes sure that when the value has changed all listeners are notified (calling method notify).
Let’s use it:
Running the above code generates the following output:
The complete sample is attached to the blog post.
The manual solution has the following disadvantages.
- It forces the developer to derive from ObservableBase class.
- observable property is handled manually
So let’s see another solution in the next post.