我想让我的淘汰赛脚本更有条理,此外,我想避免意外地将 2 个函数命名为相同的东西。所以我想知道我是否可以像这样将视图模型嵌套在同一个函数中(我保持非常简单):小提琴
这是HTML
<p>First name: <strong data-bind="text: other.firstName">todo</strong></p>
<p>Last name: <strong data-bind="text: other.lastName">todo</strong></p>
<p>Full name: <strong data-bind="text: other.fullName">todo</strong></p>
和JS:
function AppViewModel() {
var self = this;
self.other = {
firstName: ko.observable("Bert"),
lastName: ko.observable("Bertington"),
/*fullName: ko.computed(function(){
return this.firstName + " " + this.lastName;
}, this)*/
}
}
这工作正常,但如果我取消注释 ko.computed 它会崩溃。有没有办法以这种方式组织我的淘汰赛,为什么计算会崩溃,有没有办法编写 ko.computed 函数以便它可以工作?
编辑:问题 #2
如果我有这样的表格:
<form data-bind="submit: other.otherSubmit" data-ajax="false">
<button type="submit">Submit</button>
</form>
我为提交添加了一个处理程序,如下所示:
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
var self = this;
self.other = new function(){
var self = this;
self.firstName = ko.observable("Bert");
self.lastName = ko.observable("Bertington");
self.fullName = ko.computed(function(){
return self.firstName() + " " + self.lastName();
});
self.otherSumbit = function(){}
}
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
为什么错误控制台会返回这个:
提交绑定的值必须是函数