Skip to content

Commit

Permalink
#1040 if splice replaces the same item, don't unbind from it
Browse files Browse the repository at this point in the history
  • Loading branch information
moschel committed Jun 4, 2014
1 parent 3594fee commit e642970
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 15 additions & 3 deletions list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,35 @@ steal("can/util", "can/map", "can/map/bubble.js",function (can, Map, bubble) {
*/
splice: function (index, howMany) {
var args = can.makeArray(arguments),
added =[],
i;

debugger;
for (i = 2; i < args.length; i++) {
args[i] = bubble.set(this, i, this.__type(args[i], i) );

added.push(args[i]);
}
if (howMany === undefined) {
howMany = args[1] = this.length - index;
}
var removed = splice.apply(this, args);
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 (var i = 0; i < removed.length; i++) {
if(added.indexOf(removed[i]) !== -1) {
cleanRemoved.splice(i, 1);
}
};
}

if (!spliceRemovesProps) {
for (i = this.length; i < removed.length + this.length; i++) {
delete this[i];
}
}

console.log('removed', removed, cleanRemoved, added);
can.batch.start();
if (howMany > 0) {
this._triggerChange("" + index, "remove", undefined, removed);
Expand Down
13 changes: 13 additions & 0 deletions model/model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1516,4 +1516,17 @@ steal("can/model", 'can/map/attributes', "can/test", "can/util/fixture", functio
start();
});
});

test("model list destroy after calling replace", function(){
expect(2);
var map = new can.Model({name: "map1"})
var map2 = new can.Model({name: "map2"});
var list = new can.Model.List([map, map2]);
list.bind('destroyed', function(ev){
ok(true, 'trigger destroyed')
})
can.trigger(map, 'destroyed');
list.replace([map2]);
can.trigger(map2, 'destroyed');
});
});

0 comments on commit e642970

Please sign in to comment.