我有一个与父子关系的模型,我想在表格中显示每个父项,然后是子项,我会稍微缩进子项。我快到了,但我似乎只能将子项附加到父行,而不是在父行之后添加它们。
View.TreeView = Backbone.Marionette.CompositeView.extend({
template: listItemTpl,
tagName: "tr",
initialize: function() {
var children = new Backbone.Collection(this.model.get('children'));
if (this.model.get('parent')) {
this.$el.addClass('child');
} else {
this.$el.addClass('parent');
}
this.collection = children;
},
attachHtml: function(collectionView, childView, index) {
collectionView.$el.append(childView.el);
}
});
View.TreeRoot = Backbone.Marionette.CompositeView.extend({
tagName: "table",
className: "table table-bordered table-striped",
childView: View.TreeView,
template: listTpl,
childViewContainer: "tbody"
});
我试过了
collectionView.$el.after(childView.el);
但这根本行不通。
我的模型父子是使用如下 Sailsjs 创建的服务器端。
module.exports = {
schema: true,
attributes: {
name: 'string',
category: {
model: 'Category'
},
parent: {
model: 'Term'
},
children: {
collection: 'Term',
via: 'parent'
}
}
};