Created
March 26, 2014 03:11
-
-
Save mcat/9776304 to your computer and use it in GitHub Desktop.
Inserting a model at correct index in Marionette.CollectionView appendHtml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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