2012-03-23 30 views
0

所以我有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) 

回答

1

只是錯過了這一個幾件事。

在這裏你走了。

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

即。你在功能上閱讀,而不是「這一點。」有「self.theWeight」 ......

編碼快樂!

+0

還有一件事。 'self'在後面的代碼(在可編輯數據下面)工作的唯一原因是它指的是['window.self'](https://developer.mozilla.org/en/DOM/window.self)。這有點令人困惑。 – 2012-03-23 22:55:15

+0

非常感謝你!我知道這是一件小事,我做錯了。 – 2012-03-26 17:15:33

+0

馬修所指的自我並不是你應該玩的對象。將全局對象保存在命名空間(var myapplication或類似的東西)中,或者在DOM(jquery.data()對象) – 2012-03-27 17:18:19

0
alert(self.users()[0].theReadings()[0].theWeight(); 

我建議去掉 '泰晤士報'。這是一種不尋常的風格。

+0

我試着添加.theWeight()但它不起作用。它只是打破了一切。 – 2012-03-23 19:49:37