2013-03-08 161 views
1

我正在使用knockout-2.2.0.js。我有以下型號:計算可觀察問題

function Person(name, age, city) 
{ 
this.Name = ko.observable(name); 
this.Age = ko.observable(age); 
this.City = ko.observable(city); 

ko.computed(function(){ 

    //Is there any way to execute this section whenever Name, Age, City any 
    //of this observable changes without including this.Name, this.Age and 
    //this.City inside it 

}, this).extend({ throttle: 500 }); 
} 

我知道我問是有點怪,但我想知道有沒有辦法做到這一點?

+3

簡答題:不,你想達到什麼目的,也許有不同的方法來解決它,而不是黑客搗蛋? – nemesv 2013-03-08 08:01:04

+0

我認爲你只需要訂閱模型。 – Ohgodwhy 2013-03-08 08:02:15

+0

yaa訂閱是好點,但我想使用油門擴展器,我們可以在訂閱中使用它嗎?因爲我不想得到我可觀察到的實時變化 – gaurav 2013-03-08 08:03:39

回答

1

做,這是叫你計算ko.toJS(this),最簡單的方式,更新功能都會自動調用。這通常用於從視圖模型中構建「乾淨」對象。作爲構建該對象的一部分,它將打開所有可觀察對象(創建依賴關係)。那麼,只要任何依賴關係發生變化,您的計算就會被觸發。

這東西就像一個「髒標誌」的基礎:http://www.knockmeout.net/2011/05/creating-smart-dirty-flag-in-knockoutjs.html

您可以使用throttle你以這種方式計算的。