Collection.fetch retains stale models after server-side deletions without reset option
0 reputation · 13 Jan 2024, 05:13 UTC
Symptom
A Backbone.Collection backed by a REST endpoint continues to hold models that no longer exist on the server after a fetch() call. The default merge semantics reconcile incoming records by ID but never remove local models that are absent from the response.
Context
The application relies on Collection.fetch() to refresh data after server-side schema migrations or bulk deletions. Because Backbone models are schemaless, there is no built-in mechanism to detect or roll back attribute-level changes. The only way to force a full replacement is passing {reset: true}, which changes the event contract from individual add/remove events to a single reset event.
Constraints
- Existing view listeners may be bound to
add,remove, orchangeevents and not toreset. - Switching to
reset: truecan break UI updates that depend on granular events. - Backbone, Underscore, and jQuery versions in the lockfile may exhibit subtle timing differences in
synccompletion.
Questions
- What decision criteria should guide the choice between default merge and
reset: truefor a given synchronization scenario? - How can a codebase reliably support both event styles without duplicating listener logic?
- Are there version-specific behaviors in
Backbone.syncthat affect the reliability of either approach?