I am trying to sort a collection in a Marionette.CompositeView
.
I have a collection which looks like this:
[
{id: 1, name: \'bar\'},
For Marionette >= 2.0, CollectionView
and CompositeView
maintain sorting by default.
For Marionette < 2.0 and >= 1.3.0:
var MySortedView = Backbone.Marionette.CollectionView.extend({
// ...
appendHtml: function(collectionView, itemView, index) {
// Already sorted when buffering.
if (collectionView.isBuffering) {
Backbone.Marionette.CollectionView.prototype.appendHtml.apply(this, arguments);
}
else {
var childrenContainer = $(collectionView.childrenContainer || collectionView.el);
var children = childrenContainer.children();
if (children.size() === index) {
childrenContainer.append(itemView.el);
} else {
childrenContainer.children().eq(index).before(itemView.el);
}
}
}
});
For Marionette < 2.0 or < 1.3.0 (same as before without buffering):
var MySortedView = Backbone.Marionette.CollectionView.extend({
// ...
appendHtml: function(collectionView, itemView, index) {
var childrenContainer = $(collectionView.childrenContainer || collectionView.el);
var children = childrenContainer.children();
if (children.size() === index) {
childrenContainer.append(itemView.el);
} else {
childrenContainer.children().eq(index).before(itemView.el);
}
}
});
It's the same for CollectionView and CompositeView.