backbone.validation
backbone.validation copied to clipboard
Options are stored on the model, this is bad when 2 views use diff options.
Example:
class SubView extends View {
initialize() {
// Custom selector.
Validation.bind(this.model, { selector: 'data-validate' });
}
}
class MyView extends View {
initialize() {
// Standard.
Validation.bind(this.model);
}
onRender() {
// 2 views sharing the same model.
let subview = new SubView({model: this.model});
....
}
}
This breaks the parent view, because the last .binds options are the ones that are used in all views bound to the same model.
This is pretty critical as I have a lot of subviews that are basically components.
I just spent hours debugging into the validation code to come to this same conclusion. If 3 different views all call
Validation.bind(this, {
invalid: this.onInvalid.bind(this)
}
then the model becomes invalid, the same invalid function (whichever was last registered) will be called 3 times, but the correct view will be passed to it as the first parameter.
It really makes it hard to have a Marionette Behavior that reacts to validation issues. If a sub-region of a view has a Validation.bind then it prevents the parent from working correctly unless they share the same options.invalid and options.valid function.
The view bind design is just flawed. I looked into ways to fix it but would increase code complexity.
At the time i gave up and now i think is better to decouple things (do not store bound view to a model property)