Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collectionbinder sort static content #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
CollectionBinder: Update algorithm in sortRootEls to take static cont…
…ent into account and don't move it to bottom each time
  • Loading branch information
Alexey Makhrov committed Feb 19, 2015
commit ce7cc328fcad8dcf420e543dbf86f556f18ddea8
17 changes: 15 additions & 2 deletions Backbone.CollectionBinder.js
Original file line number Diff line number Diff line change
@@ -160,15 +160,28 @@
},

sortRootEls: function(){
// sorting an empty collection or a collection with a single element won't change anything
if (this._collection.length < 2) return;

var children = $(this._elManagerFactory._getParentEl()).children();
var indexes = this._collection.models.map(function (model) {
var modelElManager = this.getManagerForModel(model);
var modelEl = modelElManager.getEl();
return modelEl.index();
}, this);

var minIndex = Math.min.apply(Math, indexes);

this._collection.each(function(model, modelIndex){
var modelElManager = this.getManagerForModel(model);
if(modelElManager){
var realIndex = modelIndex + minIndex;
var modelEl = modelElManager.getEl();
var currentRootEls = $(this._elManagerFactory._getParentEl()).children();

if(currentRootEls[modelIndex] !== modelEl[0]){
if(currentRootEls[realIndex] !== modelEl[0]){
modelEl.detach();
modelEl.insertBefore(currentRootEls[modelIndex]);
modelEl.insertBefore(currentRootEls[realIndex]);
}
}
}, this);