I have a ViewModel that is structured like this:
function CallLogViewModel() {
var self = this;
// Observables
self.client = {
Id: ko.observable(),
Name: ko.observable().extend({required: true, message: 'Name is required'}),
Surname: ko.observable().extend({ required: true, message: 'Surname is required' }),
EmailAddress: ko.observable().extend({ required: true, message: 'Email is required', email: true }),
Mobile: ko.observable(),
Fax: ko.observable(),
Tel: ko.observable(),
Area: ko.observable(),
HasCar: ko.observable(),
HeardAboutMethodId: ko.observable().extend({ required: true, message: 'Please select option' }),
DateOfBirth: ko.observable().extend({ required: true, message: 'DateOfBirth is required' }),
Comments: ko.observableArray(),
Calls: ko.observableArray()
};
self.comment = {
Id: ko.observable(),
Value: ko.observable().extend({ required: true, message: 'Comment is required' }),
ClientId: ko.observable(),
Created: ko.observable()
};
self.addComment = function () {
self.comment.Created = moment().format('YYYY-MM-DD, HH:mm:ss');
self.client.Comments.push(self.comment);
self.comment.Created = '';
self.comment.Value = '';
$('#ClientCommentsTable').DataTable();
console.log(self.client.Comments());
};
};
When I add a new self.comment object into the array object it does display the comment in the table but the array object shows that the values are empty strings?
Does it have something to do with how I clear the comment object?
<>button.self.commentis not recreated everytime while it is pushed toself.client.comments. But I'd need a working example to confirm that.CreatedandValueare observables. To set the value, you need to useself.comment.Value('');andself.comment.Created(moment()....). Also, you need to create a new object using the properties ofself.comment