Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干:如何从另一个模型内的集合中获取模型属性?_Javascript_Backbone.js_Backbone Relational - Fatal编程技术网

Javascript 主干:如何从另一个模型内的集合中获取模型属性?

Javascript 主干:如何从另一个模型内的集合中获取模型属性?,javascript,backbone.js,backbone-relational,Javascript,Backbone.js,Backbone Relational,型号代码: App.Team = Backbone.RelationalModel.extend({ urlRoot: 'data/json/team', /*urlRoot: 'data/json/myteam.txt',*/ idAttribute: 'id', relations: ... app.currentTeam = new App.Team({id:11}); 视图: 从错误方法的集合中获取模型 app.currentTeam.attributes.history.get(i

型号代码:

App.Team = Backbone.RelationalModel.extend({
urlRoot: 'data/json/team',
/*urlRoot: 'data/json/myteam.txt',*/
idAttribute: 'id',
relations:
...

app.currentTeam = new App.Team({id:11});
视图:


从错误方法的集合中获取模型

app.currentTeam.attributes.history.get(i);
你必须使用

app.currentTeam.get('history') // you got collection
app.currentTeam.get('history').at(i); // you got model from collection by index
更新1: 尝试使用迭代器从集合中获取元素:

{
    type: Backbone.HasMany,
    key: 'history',
    relatedModel: 'App.HistoryItem',
    collectionType: 'App.History',
    reverseRelation: {
        key: 'team',
        includeInJSON: 'id',
    }
}
var trophiesBox = JST['teaminfo/history/leftbox'](app.currentTeam.attributes);
$("#teamhistory_leftbox").append(trophiesBox);  
app.currentTeam.get('history').each(function(historyData, i) {
    var historyRow = JST['teaminfo/history/row'](historyData.attributes);
    $("#teamhistory_table_body").append(historyRow);
}, this);

你查过了吗。这可能会有所帮助。

请显示团队和历史的关系Collection@Sergey我添加了关系。为什么使用attributes.history而不是get('history')?谢谢,我更改了代码,但仍然得到相同的错误。我想知道还有什么原因会导致它。你是否在javascript的调试器中调试你的应用程序?Chrome开发工具还是FireBug?现在的代码是:var historyData=app.currentTeam.get('history')。位于(i);并在这一行设置断点,显示app.currentTeam.get('history')上存储的waht。当它停止时,它显示:historyData:undefined,但当我按“Continue”时,它开始逐个加载每个模型。
var trophiesBox = JST['teaminfo/history/leftbox'](app.currentTeam.attributes);
$("#teamhistory_leftbox").append(trophiesBox);  
app.currentTeam.get('history').each(function(historyData, i) {
    var historyRow = JST['teaminfo/history/row'](historyData.attributes);
    $("#teamhistory_table_body").append(historyRow);
}, this);