所以我有2類,用戶和健康閱讀。我製作了一系列用戶對象,裏面有一些讀數。我只想訪問讀數組中的日期和重量,我一直試圖弄清楚這一點!請幫助這個簡單的問題是讓我瘋狂!KnockoutJS訪問一個單獨的類中的對象數組
// 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)
還有一件事。 'self'在後面的代碼(在可編輯數據下面)工作的唯一原因是它指的是['window.self'](https://developer.mozilla.org/en/DOM/window.self)。這有點令人困惑。 – 2012-03-23 22:55:15
非常感謝你!我知道這是一件小事,我做錯了。 – 2012-03-26 17:15:33
馬修所指的自我並不是你應該玩的對象。將全局對象保存在命名空間(var myapplication或類似的東西)中,或者在DOM(jquery.data()對象) – 2012-03-27 17:18:19