While working on this project, I came across a helpful section in that discusses "Determining if a property is a computed observable." In order to achieve this, I utilized the isComputed
function to validate whether a property is indeed a computed observable.
My main objective now is to eliminate computed observables from self.formItems()
before sending the data to my server.
Below is an excerpt of my code including the AJAX request used to dispatch the information back to the server:
for (var prop in self.formItems()) {
// console.log(ko.isComputed(self.formItems()[prop].isRadio)); //true (because this is computed)
// console.log(ko.isComputed(self.formItems()[prop].field_label)); //false (because this is not a computed element)
for(var form_prop in self.formItems()[prop]) {
// console.log(form_prop+': '+ko.isComputed(self.formItems()[prop][form_prop]));
if(self.formItems()[prop].hasOwnProperty(form_prop) && !ko.isComputed(self.formItems()[prop][form_prop])) {
// result_no_computed_observables[prop][form_prop] = ko.toJS(self.formItems()[prop][form_prop]);
console.log(self.formItems()[prop][form_prop]);
}
// ko.cleanNode(self.formItems()[prop][form_prop])
}
}
$.ajax({
'type': 'POST',
'url': appUrl+'/editors/saveform/'+memberListId+'.json',
'cache': false,
'dataType': 'json',
'contentType': 'application/json',
'data': ko.toJSON({
'id': theFormDetails.id(),
'name': theFormDetails.name(),
'description': theFormDetails.description(),
'success_text': theFormDetails.success_text(),
'success_redirect': theFormDetails.success_redirect(),
'form_elements': self.formItems() /* <--- I would like to remove computed observables from this*/
}),
'success': function(result) {
alert('success!!!');
},
'statusCode': {
403: function() {
alert("Your session has probably expired. Please login again.");
window.location = appUrl+"/users/login";
}
}
});
Do you have any suggestions or insights on how to tackle this issue?
Appreciate your help!