diff --git a/src/core/Base.js b/src/core/Base.js index 498e29df..846bebc9 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -392,6 +392,8 @@ this.Base = Base.inject(/** @lends Base# */{ var amount = items && items.length, append = index === undefined; index = append ? list.length : index; + if (index > list.length) + index = list.length; // Update _index on the items to be added first. for (var i = 0; i < amount; i++) items[i]._index = index + i; diff --git a/src/item/Item.js b/src/item/Item.js index dd6c7ccb..119267b3 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1271,10 +1271,18 @@ var Item = this.Item = Base.extend(Callback, { // an Item#children array. Use Array.prototype.slice because // in certain cases items is an arguments object items = items && Array.prototype.slice.apply(items); - var i = index; + var children = this._children, + length = children.length, + i = index; for (var j = 0, l = items && items.length; j < l; j++) { - if (this.insertChild(i, items[j], _cloning)) - i++; + if (this.insertChild(i, items[j], _cloning)) { + // We need to keep track of how much the list actually grows, + // bcause we might be removing and inserting into the same list, + // in which case the size would not chage. + var newLength = children.length; + i += newLength - length; + length = newLength; + } } return i != index; },