Skip to content

Commit

Permalink
Merge pull request #4292 from Noodlex/fix_model_event_changeId
Browse files Browse the repository at this point in the history
Trigger the "changeId" event only if prevId is different from this.id.
  • Loading branch information
jgonggrijp authored Sep 2, 2024
2 parents 4e64208 + 06ffc5d commit 8f0285f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 8f0285f

Please sign in to comment.