我有一个基本的 View 模型如下:
var myApp = myApp || {};
myApp.BaseViewModel = function () {
self = this;
self.var1 = ko.observable();
self.messageToSay = Function() {
alert("in Base view model");
};
self.sayMessage = function() {
self.messageToSay();
}
};
我有一个继承自 BaseViewModel 的 View 模型,如下所示:
var myApp = myApp || {};
myApp.HomeViewModel = function() {
var self = this;
ko.utils.extend(self. new myApp.BaseviewModel());
self.messageToSay = Function() {
alert("in Home view model");
};
};
var vm = new myApp.HomeViewModel();
ko.applyBindings(vm, vm);
问题是当我执行 vm.sayMessage() 时总是返回“在基本 View 模型中”而不是“在主视图模型中”。如何获取 HomeViewModel 中的 messageToSay() 方法以覆盖 BaseViewModel() 中的方法?
谢谢 马丁
最佳答案
这是你应该执行继承的方式:
myApp.HomeViewModel = function() {
var self = this;
myApp.BaseviewModel.apply(self, [/*BaseViewModel constructor args */]);
self.messageToSay = function() {
alert("in Home view model");
};
};
希望对你有帮助。
关于带有 knockout.js 函数覆盖的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17321250/