I'm building a huge page that has 10 different sections on it and am using Knockout for event binding, etc.
Each sections contains a form with it's own viewModel with it's fields and validation properties, etc. I patterned it after reading this post regarding multi-view models.
I have a masterViewModel that imports lots of subViewModels. This is all working fine and I can set up observable elements that auto-populate upon field input, etc.
I am binding my form submit to a function in my viewModel like below.
After I validate and save the form fields (via ajax post), I want to put the section into a read-only mode, but I don't know how to get a handle on my viewModel in my ajax call's success callback.
<form action="webservice.php" method="POST" data-bind="submit: contactInformation.validateSubmit">
this.validateSubmit = function(formElement){
var result = ko.validation.group(this, {deep: true});
if (!this.isValid()) {
result.showAllMessages(true);
return false;
}
//actually save stuff, call ajax, submit form, etc;
// setup a promise
var posting = $.post( "./webservice.php", $(formElement).serialize() );
posting.done(function( data ) {
this.contactInformation.model_state("summary"); // Uncaught TypeError: Cannot call method 'model_state' of undefined
// i also tried the line below, instead of line above...
ko.mapping.updateFromJS(this, data); // Uncaught TypeError: Cannot call method 'updateFromJS' of undefined
});
};
Does anyone have any idea how to do this? I have a handle of the formElement from the contactInformation.validateSubmit() function. Do I need to manually subscribe to a listener somewhere? Or is there a way to hang the model off of $(formElement).data('model')?
Any help would be welcomed.
Thanks, -- Scott