Hi gaffe,
Yes, I wasn't very clear in my description.
Currently a KO observable is simply a function that you can call to retrieve or set the observed model value.
Attached directly onto that function are other functions. These can be called to subscribe to the observable
and query how many subscriptions there are to the observable etc.
Each time a KO observable is created all these function bodies are copied by the browser as the browser has
no way of optimizing this object creation, it doesn't know that these functions will stay the same so it doesn't need
to create a copy of the functions each time.
Apart from that issue it does mean these observables aren't open for extension.
What I proposed was to use observables which are open for extension by having observables with normal constructors
and using the prototype chain, this also has the advantage that each observable will use less memory as functions
on the prototype will not be cloned each time you use the constructor.
The observables would be in the style I gave above.
view.knockout.KnockoutObservable = function()
{
/** @private */
this._subscriptions = {};
/** @private */
this.__ko_proto__ = ko.observable;
};
/** @private */
view.knockout.KnockoutObservable.prototype.subscribe = function (callback, callbackTarget, event)
{
//code.
}
This class basically pretends to be a normal KO observable function with attached functions.
The advantages for me are
1) It's OOP.
2) The objects take less memory.
3) My code is not coupled to KO.
To explain 3; most people when using KO write code like this for their observables
this.age = ko.observable(10);
This means that their code is tightly coupled to KO.
What I would tend to do instead is this.
this.age = presentation.model.Property(10);
with the presentation.model.Property class being my own presentation class.
Now how I get KO to accept this weird observable is I extend my view.knockout.KnockoutObservable class inside my presentation.mode.Presentation class.
I wrote the documentation that explains how to do extension on MDN (
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create )
if you want to read up on it.
This means I have very loose coupling to KO for my presentation code and could in theory easily substitute out KO for Angular or another framework if need be.
This allows me to support clients who want to use whatever framework they are familiar with as opposed to imposing one on them.
I basically have one line in my Presentation class that says it extends the KO observable class I created.
This does mean I must make a few changes to KO though for it to use methods on my class instead of simply calling the object as if it where a function
i.e. value.getValueForKnockout() instead of value()
The changes are minor though. I would like to have support for class based observables in KO if I could though. Does this help explain my question better?
B.