I have an array of items returned from a service. I am trying to determine the calculated observable for each instance of the element, so my instinct tells me to put it in the prototype.
One case for a calculated observable: the system calculates the points, but the user can choose to override the calculated value. I need to save the calculated value in case the user removes the override. I also need to combine the user assigned and calculated points and summarize the totals.
I use matching to do the following:
var itemsViewModel; var items = [ { 'PointsCalculated' : 5.1 }, { 'PointsCalculated' : 2.37, 'PointsFromUser' : 3 } ]; var mapping = { 'Items' : { create : function(options) { return new Item(options.data); } } }; var Item = function(data) { var item = this; ko.mapping.fromJS(data, mapping, item); }; Item.prototype.Points = function () { var item = this; return ko.computed(function () {
Now, how this works, I have to call an anonymous function to return the computed observable. It seems that a new instance of the computed observable is created for each binding, which affects most of the point placing it in the prototype. And it's a little annoying to decipher how many parentheses to use each time I access the observable.
It is also somewhat fragile. If I try to access Points () in code, I cannot do
var points = 0; var p = item.Points; if (p && typeof p === 'function') { points += p(); }
because it changes in the context of Points () to a DOMWindow, not an element.
If I put the computed in create () in the mapping, I could capture the context, but then there is an instance of the method for each instance of the object.
I found the post by Michael Best Google Groups ( http://groups.google.com/group/knockoutjs/browse_thread/thread/8de9013fb7635b13 ). The prototype returns a new computed observable “activate”. I did not understand what causes “activate” (maybe Objs?), But I suppose that this happens once for the object anyway, and I don’t know how much 'this' will get.
At this stage, I believe that I have passed by what is available in the published documents, but I am still working on deciphering what comes from the source.