I'm using Objs (
https://github.com/tekool/objs), but the problem you
describe comes from Javascript itself. This is what I've set up so I
can specify observables in the prototype:
/**
* Knockout prototyping helpers
*/
(function(ko) {
var addExtenders = function(extenders) { this.extenders =
extenders; };
ko.observable.proto = function(initialValue) {
return {
v: initialValue,
extend: addExtenders,
activate: function() {
return ko.observable(initialValue);
}
};
}
ko.observableArray.proto = function() {
return {
extend: addExtenders,
activate: function() {
return ko.observableArray();
}
};
}
ko.computed.proto = function(readFunction) {
return {
extend: addExtenders,
activate: function(binding) {
return ko.computed(readFunction, binding,
{deferEvaluation:true});
}
};
}
})(ko);
/**
* Base classes
*/
var ViewBase = Objs('View', {
initialize : function() {
for (var prop in this) {
if (this[prop] && this[prop].activate) {
this[prop] =
this[prop].activate(this).extend(this[prop].extenders);
}
}
}
});
/* example of usage */
var Page = Objs('View.Page', 'View', {
initialize : function(id, title) {
Page.$super.initialize.call(this);
this.id = id;
this.title = title;
this.parent = null;
},
isCurrent: ko.observable.proto(false),
isLoaded: ko.observable.proto(false),
isVisible: ko.computed.proto(function() {
return this.isCurrent() && this.isLoaded();
})
});