型號代碼:Backbone:如何從另一個模型中的集合中獲取模型屬性?
App.Team = Backbone.RelationalModel.extend({
urlRoot: 'data/json/team',
/*urlRoot: 'data/json/myteam.txt',*/
idAttribute: 'id',
relations:
...
app.currentTeam = new App.Team({id:11});
查看:
var trophiesBox = JST['teaminfo/history/leftbox'](app.currentTeam.attributes);
$("#teamhistory_leftbox").append(trophiesBox);
for (var i = 1; i <= app.currentTeam.attributes.history.length; i++) {
var historyData = app.currentTeam.attributes.history.get(i);
var historyRow = JST['teaminfo/history/row'] (historyData.attributes);
$("#teamhistory_table_body").append(historyRow);
}
我在var historyRow = JST['teaminfo/history/row'] (historyData.attributes);
線越來越"Uncaught TypeError: Cannot read property 'attributes' of undefined"
。
在我定義historyData時出現問題之前,可能是因爲它是另一個模型(app.currentTeam
)的集合中的模型(app.currentTeam.attributes.history
)。我得到了[Object] (app.currentTeam.attributes.history) doesn't have a 'get' method
類型的錯誤。現在它傳遞良好,但在下一行中我收到了另一個錯誤消息,所以我想知道我的代碼在這裏出了什麼問題。
app.currentTeam.attributes
加載罰款,所以我想有檢索模型是另一個模型中的集合內的屬性的問題。
編輯:團隊和歷史收集的關係:
{
type: Backbone.HasMany,
key: 'history',
relatedModel: 'App.HistoryItem',
collectionType: 'App.History',
reverseRelation: {
key: 'team',
includeInJSON: 'id',
}
}
請出示團隊和歷史收藏 – Sergey
的關係@Sergey我加了關係。 –
爲什麼你使用attributes.history而不是get('history')? – Sergey