Skip to content

Commit

Permalink
Remove+Change events now fired when doing a remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcox committed Jun 2, 2012
1 parent 0572fb3 commit 51ed636
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
11 changes: 9 additions & 2 deletions backbone-nested.js
Expand Up @@ -98,7 +98,14 @@
this.set(aryPath, val, opts);

if (trigger){
this.trigger('remove:' + Backbone.NestedModel.createAttrStr(aryPath), this, oldEl);
var attrStr = Backbone.NestedModel.createAttrStr(aryPath);
this.trigger('remove:' + attrStr, this, oldEl);
this.trigger('change:' + attrStr, this, oldEl);
for (var aryCount = aryPath.length - 1; aryCount >= 0; aryCount--) {
attrStr = Backbone.NestedModel.createAttrStr(_.first(aryPath, aryCount));
this.trigger('change:' + attrStr, this, oldEl);
};
this.trigger('change', this, oldEl);
}

return this;
Expand Down Expand Up @@ -152,7 +159,7 @@

// Trigger Remove Event if array being set to null
if (value === null){
var parentPath = _.initial(attrPath).join('.');
var parentPath = Backbone.NestedModel.createAttrStr(_.initial(attrPath));
model._delayedTrigger('remove:' + parentPath, model, val[attr]);
}

Expand Down
38 changes: 38 additions & 0 deletions test/nested-model.js
Expand Up @@ -452,6 +452,44 @@ $(document).ready(function() {
ok(callbacksFired[2], "'remove:addresses' should fire");
});

test("change+remove when removing from array", function() {
var callbacksFired = [false, false, false];

doc.bind('change', function(){ callbacksFired[0] = true; });
doc.bind('change:addresses', function(){ callbacksFired[1] = true; });
doc.bind('remove:addresses', function(){ callbacksFired[2] = true; });

doc.remove('addresses[1]');

ok(callbacksFired[0], "'change' should fire");
ok(callbacksFired[1], "'change:addresses' should fire");
ok(callbacksFired[2], "'remove:addresses' should fire");
});

test("change+remove when removing from deep array", function() {
var callbacksFired = [false, false, false, false, false];

doc.set('name.middle', {
initial: 'L',
full: 'Limburger',
fullAlternates: ['Danger', 'Funny', 'Responsible']
});

doc.bind('change', function(){ callbacksFired[0] = true; });
doc.bind('change:name', function(){ callbacksFired[1] = true; });
doc.bind('change:name.middle', function(){ callbacksFired[2] = true; });
doc.bind('change:name.middle.fullAlternates', function(){ callbacksFired[3] = true; });
doc.bind('remove:name.middle.fullAlternates', function(){ callbacksFired[4] = true; });

doc.remove('name.middle.fullAlternates[1]');

ok(callbacksFired[0], "'change' should fire");
ok(callbacksFired[1], "'change:name' should fire");
ok(callbacksFired[2], "'change:name.middle' should fire");
ok(callbacksFired[3], "'change:name.middle.fullAlternates' should fire");
ok(callbacksFired[4], "'remove:name.middle.fullAlternates' should fire");
});


// ----- CHANGED_ATTRIBUTES --------

Expand Down

0 comments on commit 51ed636

Please sign in to comment.