Durandal计算原型方法

时间:2016-09-30 12:16:07

标签: javascript knockout.js durandal

是否可以在我的视图模型原型上定义计算属性?据我所知,在声明时无法访问正确的this值。

我可以把它放在构造函数中,但是如你所知,它会为每个实例重新定义。所以最后这只是一个表现问题。

我的视图模型的AMD模块:

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;

1 个答案:

答案 0 :(得分:2)

  

是否可以在我的视图模型原型上定义计算属性?据我所知,在声明时无法访问正确的this值。

正确。所以答案是否定的,你不能在原型方法上使用计算机。