0

So i have 2 classes, users and health readings. i made a array of user objects with an array of readings inside it.i just want access to the date and weight in the reading array, i have been trying for a long time to figure this out! please help this simple problem is driving me nuts!

// Class to represent a row in the seat reservations grid
function Reading(theDate,theWeight)
{

    self.theDate=ko.observable(theDate);
    self.theWeight=ko.observable(theWeight);
}

function User(name,weight,date) {
    var self = this;
    self.name = name;
    self.date = date;
    self.weight = ko.observable(weight);
     self.theReadings = ko.observableArray([
        new Reading(12,13)
    ]);

}
    // Editable data
      self.users = ko.observableArray([
        new User("George",1,2012),
        new User("Bindu",2,2012)
    ]);
    /this alerts an object but i dont know how to access the weight/date variable
    alert(self.users()[0].theReadings()[0]);

    self.readings = ko.observableArray([
        new Reading(12,13)

2 Answers 2

1

Just missed a few things on this one.

Here ya go.

http://jsfiddle.net/EN7y4/3/

Namely. You had "self.theWeight" in function Reading instead of "this."...

Happy Coding!

Sign up to request clarification or add additional context in comments.

3 Comments

One more thing. The only reason self works in the later code (below Editable data) is that it refers to window.self. This is somewhat confusing.
wow thank you so much! i knew it was something small i was doing wrong.
self, as matthew was refering, is not an object you should play with. It would be smart to hold your global objects within a namespace (var myapplication, or something like that), or within the DOM (jquery.data() objects)
0
alert(self.users()[0].theReadings()[0].theWeight();

I would recommend removing the 'thes'. That's an uncommon style.

1 Comment

i tried adding the .theWeight() but it doesnt work. it kind of just breaks everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.