Update: I added a jsFiddle of my problem. I also completely rewrote the question as ideas have changed.
I have a page where I am using knockout.js for the layout. I have this working successfully on another page in my application, but I can't get this one to work. My model consists of a parent record and it's child records. My problem is when I try to make copy of the previous row to make it easier for the user to add many similar records. When I type values in the my Department, Project, Job, and Comment fields, the values are blank in the model so when I go to copy those fields, they remain blank.=. Any ideas? I am guessing I am missing something stupid.
function DayViewModel(day) {
var self = this;
self.Date = day.Date;
self.Time = day.Time;
self.Hours = ko.observable(day.Hours);
self.TimesheetCode = ko.observable(day.TimesheetCode);
self.Department = ko.observable(day.Department);
self.Project = ko.observable(day.Project);
self.Job = ko.observable(day.Job);
self.Comments = ko.observable(day.Comments);
self.JobIsRequired = ko.observable((day.JobIsRequired == undefined) ? false : day.JobIsRequired);
}
function RequestViewModel() {
var self = this;
self.DaysRequested = ko.observableArray([new DayViewModel({
Date: new Date().toString("MM/dd/yyyy"),
Time: "08:00 AM",
Hours: 1,
TimesheetCode: "",
Department: 0,
Project: 0,
Job: 0,
Comments: ""
})]);
self.timesheetCodes // gets data from an external data source
// Add another day copying the previous day.
self.addDay = function () {
var array = self.DaysRequested();
var previousDay = array[array.length - 1];
previousDay.Date = Date.parse(previousDay.Date).addDays(1).toString("MM/dd/yyyy");
var test = ko.utils.unwrapObservable(previousDay.Department);
var newDay = new DayViewModel({
Date: previousDay.Date.toString("MM/dd/yyyy"),
Time: previousDay.Time,
Hours: ko.utils.unwrapObservable(previousDay.Hours),
Department: ko.utils.unwrapObservable(previousDay.Department),
Project: ko.utils.unwrapObservable(previousDay.Project),
Job: ko.utils.unwrapObservable(previousDay.Job),
Comments: ko.utils.unwrapObservable(previousDay.Comments),
TimesheetCode: ko.utils.unwrapObservable(previousDay.TimesheetCode)
});
self.DaysRequested.push(newDay);
}
}