Collection.add accepts negative indexes for the at option

This commit is contained in:
James Hartig
2014-11-14 16:05:52 -05:00
committed by Adam Krebs
parent 04f912ca38
commit 8569f8985c
2 changed files with 8 additions and 0 deletions

View File

@@ -700,6 +700,7 @@
models = singular ? (models ? [models] : []) : models.slice();
var id, model, attrs, existing, sort;
var at = options.at;
if (at < 0) at += this.length + 1;
var sortable = this.comparator && (at == null) && options.sort !== false;
var sortAttr = _.isString(this.comparator) ? this.comparator : null;
var toAdd = [], toRemove = [], modelMap = {};

View File

@@ -1482,4 +1482,11 @@
collection.set([{id: 1}, {id: 2}, {id: 3}]);
});
test("add supports negative indexes", 1, function() {
var collection = new Backbone.Collection([{id: 1}]);
collection.add([{id: 2}, {id: 3}], {at: -1});
collection.add([{id: 2.5}], {at: -2});
equal(collection.pluck('id').join(','), "1,2,2.5,3");
});
})();