knockout.js - knockoutjs - Ajax Form Submit and update viewModel in success callback -
i'm building huge page has 10 different sections on , using knockout event binding, etc.
each sections contains form it's own viewmodel it's fields , validation properties, etc. patterned after reading this post regarding multi-view models.
i have masterviewmodel imports lots of subviewmodels. working fine , can set observable elements auto-populate upon field input, etc.
i binding form submit function in viewmodel below.
after validate , save form fields (via ajax post), want put section read-only mode, don't know how handle on viewmodel in 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 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 // tried line below, instead of line above... ko.mapping.updatefromjs(this, data); // uncaught typeerror: cannot call method 'updatefromjs' of undefined }); };
does have idea how this? have handle of formelement contactinformation.validatesubmit() function. need manually subscribe listener somewhere? or there way hang model off of $(formelement).data('model')?
any welcomed.
thanks, -- scott
maybe can structure call little differently form submit tied function on master view model
<form data-bind="submit: submitcontactinformation">
then, in view model, make call sub view model separately:
var viewmodel = function () { var self = this; self.contactinformation = ...import sub view model self.iscontactinformationreadonly = ko.observable(false); self.submitcontactinformation= function () { //make ajax call if( contactinformation.validatesubmit() ){ self.iscontactinformationreadonly(true); } }; }; var vm = new viewmodel(); ko.applybindings(vm);
Comments
Post a Comment