Merge pull request #4292 from Noodlex/fix_model_event_changeId

Trigger the "changeId" event only if prevId is different from this.id.
This commit is contained in:
Julian Gonggrijp
2024-09-02 14:55:04 +02:00
committed by GitHub
2 changed files with 14 additions and 1 deletions

View File

@@ -523,7 +523,9 @@
if (this.idAttribute in attrs) {
var prevId = this.id;
this.id = this.get(this.idAttribute);
this.trigger('changeId', this, prevId, options);
if (this.id !== prevId) {
this.trigger('changeId', this, prevId, options);
}
}
// Trigger all relevant attribute changes.

View File

@@ -1476,4 +1476,15 @@
assert.equal(model.id, 3);
});
QUnit.test('#4289 - Trigger "changeId" need to be generate only if the content id change', function(assert) {
assert.expect(1);
var model = new Backbone.Model({id: 1});
model.idAttribute = 'id';
model.on('changeId', function(m) {
assert.equal(m.get('id'), 2);
});
model.set({id: 1});
model.set({id: 2});
});
})(QUnit);