I'd like to keep my knockout script more organized, moreover, I'd like to avoid naming 2 functions the same thing by accident. So I was wondering if I could nest viewModels in the same function like so (I kept it real simple): Fiddle
Here's the 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>
and the 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)*/
}
}
This works fine, but if I uncomment the ko.computed it'll crash. Is there any way to organize my knockout in this way, why is the computed crashing, is there any way to write the ko.computed function so it will work?
EDIT: Problem #2
If I have a form any form like this:
<form data-bind="submit: other.otherSubmit" data-ajax="false">
<button type="submit">Submit</button>
</form>
and I add a handler for submit like so:
// 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());
why does the error console return this:
The value for a submit binding must be a function