Johan Johan - 2 months ago 19
Javascript Question

Durandal computed prototype methods

Is it possible to define a computed property on my view model prototype? As far as I can tell, there is no way to access a proper

this
value at the time of declaration.

I can put it inside the constructor, but then it'll be redefined for each instance as you know. So in the end this is just a question of performance.

My AMD module for my view model:

function viewModel(){
// this.someComputed works
}

viewModel.prototype.someComputed = ko.pureComputed(function(){

}/*, no context yet..? */);

viewModel.prototype.activate = function(){
// 'this' is an instance of the view model (I presume)
// should I attach it here?
};

return viewModel;

Answer

Is it possible to define a computed property on my view model prototype? As far as I can tell, there is no way to access a proper this value at the time of declaration.

Correct. So the answer is no, you can't use computeds on prototype methods.

Comments