Inconsistent event ordering during model validation failure
0 reputation · 11 Oct 2021, 14:00 UTC
In Backbone models, the validate callback is designed to intercept attribute changes during set() or save(). If the callback returns an object or throws an error, the model is supposed to prevent the update and trigger the error event.
However, there is technical ambiguity regarding the deterministic sequence of event firing when validation fails. While the attribute change is expected to be rolled back, the temporal relationship between the error event and any potential internal state changes or secondary listeners is not strictly defined across all browser environments in the Backbone 1.x branch.
This behavior creates race conditions when writing unit tests that rely on the state of the model immediately following a failed set operation.
- Is the
errorevent guaranteed to fire before anychangeevents if validation fails? - Does the model remain entirely untouched if the
validatefunction throws a exception exception versus returning an error object?