我想将代码添加到函数之外,甚至可能在一个完全不同的 JS 文件中。这可能吗?我怎样才能做到这一点?这是一个代表我迄今为止尝试过的代码片段:
function myViewModel() {
var self = this;
this.firstName = ko.observable("Mike");
this.lastName = ko.observable("Rassel");
// this was where I was originally making the call
//myViewModel.fullName = ko.computed(function() {
// return self.firstName() + ' ' + self.lastName();
})
}
// this is where the error is happening
myViewModel.fullName = ko.computed(function() {
return self.firstName() + ' ' + self.lastName();
ko.applyBindings(new myViewModel());
更多代码可以在这个 JFiddle查看。