Skip to content

Instantly share code, notes, and snippets.

@mcat
Created March 26, 2014 03:11
Show Gist options
  • Save mcat/9776304 to your computer and use it in GitHub Desktop.
Save mcat/9776304 to your computer and use it in GitHub Desktop.
Inserting a model at correct index in Marionette.CollectionView appendHtml
/*
* @author Dave Taylor https://github.com/davetayls
* @see http://davetayls.me/blog/2013/10/03/model-index-in-marionette-collectionview-appendhtml/
*/
Marionette.CollectionView.extend({
appendHtml: function(collectionView, itemView, index) {
var childAtIndex;
// could just quickly
// use prepend
if (index === 0) {
return collectionView.$el
.prepend(itemView.el);
} else {
// see if there is already
// a child at the index
childAtIndex = collectionView.$el
.children().eq(index);
if (childAtIndex.length) {
return childAtIndex
.before(itemView.el);
} else {
return collectionView.$el
.append(itemView.el);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment