我正在开发我的第一个 Backbone.js 应用程序,并且遇到了一些奇怪的行为,我担心这可能表明我的设计存在问题。我的数据如下所示:
Syllabus
Dance
Figure
Figure
Figure
Dance
Figure
Figure
etc.
我创建了这个模型来表示它:
$.syllabus.Syllabus = Backbone.RelationalModel.extend({
urlRoot: '/api/syllabus',
idAttribute: 'id',
relations: [{
type: Backbone.HasMany,
key: 'danceAssignments',
relatedModel: '$.syllabus.DanceAssignment',
collectionType: '$.syllabus.DanceAssignmentCollection',
reverseRelation: {
key: 'syllabus',
includeInJSON: 'id'
}
}]
});
$.syllabus.DanceAssignment = Backbone.RelationalModel.extend({
urlRoot: '/api/danceassignments',
idAttribute: 'id',
relations: [{
type: Backbone.HasMany,
key: 'figureAssignments',
relatedModel: '$.syllabus.FigureAssignment',
collectionType: '$.syllabus.FigureAssignmentCollection',
reverseRelation: {
key: 'danceAssignment',
includeInJSON: 'id'
}
}],
});
$.syllabus.DanceAssignmentCollection = Backbone.Collection.extend({
model: $.syllabus.DanceAssignment,
urlRoot: '/api/danceassignments',
comparator: function(danceAssignment) {
return danceAssignment.get('index');
},
});
$.syllabus.FigureAssignment = Backbone.RelationalModel.extend({
urlRoot: '/api/figureassignments',
idAttribute: 'id'
});
$.syllabus.FigureAssignmentCollection = Backbone.Collection.extend({
model: $.syllabus.FigureAssignment,
url: '/api/figureassignments',
comparator: function(figureAssignment) {
return figureAssignment.get('index');
},
});
当我更改其成员之一的 index 属性时,FigureAssignmentCollection 会自动排序,但 DanceAssignmentCollection 不会,当我明确告诉它时它也不会排序。比较器被调用,但是如果我在排序之后打印出集合的内容,它们的顺序是错误的,并且界面会使它们乱序。
有什么想法吗?