mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Collection.add accepts negative indexes for the at option
This commit is contained in:
@@ -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 = {};
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user