我正試圖在Hottowel SPA模板中的viewmodal模式之後創建一個簡單的挖空計算observable。什麼是最好方式做到這一點?在hottowel SPA中計算可觀察的敲除默認視圖模型
我本來是這樣的:
define(['services/logger'], function (logger) {
var vm = {
activate: activate,
title: 'Home View',
testField: ko.observable("This is a test"),
testComputedField: ko.computed(getComputed, this)
};
return vm;
//#region Internal Methods
function getComputed() {
return "Computed: " + this.testField();
}
function activate() {
logger.log('Home View Activated', null, 'home', true);
return true;
}
//#endregion
});
但這會導致一個錯誤,雖然我不是100%肯定,爲什麼
TypeError: this.testField is not a function
帶着幾分試驗和錯誤我的
所以得到這個:
define(['services/logger'], function (logger) {
var vm = {
activate: activate,
title: 'Home View',
testField: ko.observable("This is a test")
};
vm.testComputedField = ko.computed(getComputed, vm);
return vm;
//#region Internal Methods
function getComputed() {
return "Computed: " + this.testField();
}
function activate() {
logger.log('Home View Activated', null, 'home', true);
return true;
}
//#endregion
});
但我不知道這是一個非常漂亮的方式做到這一點;我很明顯沒有在HotTowel中熟練使用視圖模型的模塊模式。所以我的問題是:
爲什麼我的原始方法不工作? 有沒有比我的第二種方法更好或替代方式來定義\構建視圖模型?
謝謝,這看起來像一個更好的方法。 – mutex 2013-03-29 08:42:14