0
我有兩個敲除變量'Frequency'和'FrequencyText',它們只是與頻率相關的文本描述。設置ko只在加載時計算並保存
在我的模型,我有:
self.Frequency = ko.observable();
self.FrequencyText = ko.computed(function() {
var description;
switch (self.Frequency()) {
case "1":
description = "Once";
break;
case "2":
description = "Twice";
break;
case "3":
description = "Three Times";
break;
default:
description = "";
break;
}
return description;
}, this);
//Ajax call
GetAjax.done(function (data) {
self.Frequency(data.Frequency);
});
頻率由阿賈克斯加載和FrequencyText隨之移動。但是,如果用戶使用單選按鈕改變頻率,文本也會改變,我只希望在頻率保存到配置文件時更改頻率。所以我想在頁面加載時進行設置,並且如果他們保存了任何更改,就再次設置它。要做到這一點,我可以把它改成這樣的:
self.Frequency = ko.observable();
self.FrequencyText = ko.observable();
GetAjax.done(function (data) {
self.Frequency(data.Frequency);
self.FrequencyText(ko.computed(function() {
var description;
switch (self.Frequency()) {
case "1":
description = "Once";
break;
case "2":
description = "Twice";
break;
case "3":
description = "Three Times";
break;
default:
description = "";
break;
}
return description;
}, this));
});
因此,它只能從JSON數據集(等負載),但是標識要重複所有的代碼在保存功能非常新的頻率。
我試圖離開ko.computed功能訪問Ajax像第一個例子,只是把它傳遞參數上載和提交更新它喜歡:
self.FrequencyText = ko.computed(function (newFrequency) {
//switch logic
});
但它說我有使用write:語句來使用參數。這就是我陷入困惑並來到這裏的地步。有關如何清理這一點的任何幫助?
啊,但當然,這是我得到的嘗試過度使用淘汰賽,謝謝 – DasBeasto