Skip to content

Commit

Permalink
Merge pull request #1278 from bitovi/1277-splice-events
Browse files Browse the repository at this point in the history
#1277 fixes splice events
  • Loading branch information
justinbmeyer committed Oct 17, 2014
2 parents a0054df + b03a700 commit 819bb1a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
31 changes: 16 additions & 15 deletions list/list.js
Expand Up @@ -272,26 +272,22 @@ steal("can/util", "can/map", "can/map/bubble.js",function (can, Map, bubble) {
splice: function (index, howMany) {
var args = can.makeArray(arguments),
added =[],
i, j;
for (i = 2; i < args.length; i++) {
args[i] = bubble.set(this, i, this.__type(args[i], i) );
i, len;

// converting the arguments to the right type
for (i = 2, len = args.length; i < len; i++) {
args[i] = this.__type(args[i], i);
added.push(args[i]);
}

// default howMany if not provided
if (howMany === undefined) {
howMany = args[1] = this.length - index;
}
var removed = splice.apply(this, args),
cleanRemoved = removed;

// remove any items that were just added from the removed array
if(added.length && removed.length){
for (j = 0; j < removed.length; j++) {
if(can.inArray(removed[j], added) >= 0) {
cleanRemoved.splice(j, 1);
}
}
}
var removed = splice.apply(this, args);

// delete properties for browsers who's splice sucks (old ie)
if (!spliceRemovesProps) {
for (i = this.length; i < removed.length + this.length; i++) {
delete this[i];
Expand All @@ -300,11 +296,16 @@ steal("can/util", "can/map", "can/map/bubble.js",function (can, Map, bubble) {

can.batch.start();
if (howMany > 0) {
this._triggerChange("" + index, "remove", undefined, removed);
// tears down bubbling
bubble.removeMany(this, removed);
this._triggerChange("" + index, "remove", undefined, removed);
}
if (args.length > 2) {
this._triggerChange("" + index, "add", args.slice(2), removed);
// make added items bubble to this list
for (i = 0, len = added.length; i < len; i++) {
bubble.set(this, i, added[i]);
}
this._triggerChange("" + index, "add", added, removed);
}
can.batch.stop();
return removed;
Expand Down
8 changes: 8 additions & 0 deletions list/list_test.js
Expand Up @@ -225,5 +225,13 @@ steal("can/util", "can/list", "can/test", "can/compute", function(){

equal(list.length, 2);
});

test('Dispatch correct arguments with can.List:splice if inserting and removing the same elements.(#1277)', function(){
var list = new can.List(["a","b"]);
list.bind("remove", function(ev, items, index){
equal(items.length, 2);
});
list.splice(0,2,"a","b");
});

});

0 comments on commit 819bb1a

Please sign in to comment.