As a newcomer to knockout, I am excited to dive into it head first. Here's my issue: I'm attempting to access a list (array) of objects within another list of objects (array).
For example: (to keep it simple)
Teacher -> Students
Teacher #1
- Student 1
- Student 2
- Student 3
Teacher #2
- Student 1
- Student 2
- Student 3
Teacher #3
- Student 1
- Student 2
- Student 3
While I've successfully displayed the list of all teachers, when it comes to displaying the list of students, the last node (Teacher #3) shows all the students from Teachers (#1, #2, and #3); while Teacher #1 and #2 are blank.
var ViewModel = {
Teachers: ko.observableArray([])
}
function LoadTeachers(....) //Here Teacher list is loaded successfully.>
ko.applyBindings(ViewModel);
function teacher(T){
this.TeacherID:ko.observable(T.TeacherID);
this.TeacherName: ko.observable(T.TeacherName);
this.StudentArray = ko.observableArray([]);
function student(s){
this.StudentID=ko.observable(s.StudentID);
this.Name = ko.observable(s.s.Name);
}
$.getJson('...'); // here is where Student array is loaded.
}
then I would have in my view page:
foreach: Teacher
foreach: Student
Drawing on my C# knowledge and applying it to knockout, I suspect the issue lies in declaring the Student Array inside the teacher object, since each student belongs to a specific teacher. In C#, this is how I would approach it.
Is there a way to make this coding work? I hope so!
Thank you in advance