Skip to content

Commit 8f0285f

Browse files
authored
Merge pull request #4292 from Noodlex/fix_model_event_changeId
Trigger the "changeId" event only if prevId is different from this.id.
2 parents 4e64208 + 06ffc5d commit 8f0285f

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

backbone.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,9 @@
523523
if (this.idAttribute in attrs) {
524524
var prevId = this.id;
525525
this.id = this.get(this.idAttribute);
526-
this.trigger('changeId', this, prevId, options);
526+
if (this.id !== prevId) {
527+
this.trigger('changeId', this, prevId, options);
528+
}
527529
}
528530

529531
// Trigger all relevant attribute changes.

test/model.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,4 +1476,15 @@
14761476
assert.equal(model.id, 3);
14771477
});
14781478

1479+
QUnit.test('#4289 - Trigger "changeId" need to be generate only if the content id change', function(assert) {
1480+
assert.expect(1);
1481+
var model = new Backbone.Model({id: 1});
1482+
model.idAttribute = 'id';
1483+
model.on('changeId', function(m) {
1484+
assert.equal(m.get('id'), 2);
1485+
});
1486+
model.set({id: 1});
1487+
model.set({id: 2});
1488+
});
1489+
14791490
})(QUnit);

0 commit comments

Comments
 (0)