mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-19 21:13:28 -07:00
can display server-side errors on the UI.
This commit is contained in:
parent
43baa2b86e
commit
9195dc6de5
7 changed files with 740 additions and 3 deletions
79
UI/Mixins/AsValidatedView.js
Normal file
79
UI/Mixins/AsValidatedView.js
Normal file
|
@ -0,0 +1,79 @@
|
|||
define(
|
||||
[
|
||||
'backbone.validation',
|
||||
'underscore',
|
||||
'jQuery/Validation'
|
||||
], function (Validation, _) {
|
||||
'use strict';
|
||||
|
||||
return function () {
|
||||
|
||||
var originalOnRender = this.prototype.onRender;
|
||||
var originalOnClose = this.prototype.onClose;
|
||||
var originalBeforeClose = this.prototype.onBeforeClose;
|
||||
|
||||
|
||||
this.prototype.onRender = function () {
|
||||
|
||||
Validation.bind(this);
|
||||
|
||||
|
||||
if (!this.originalSync && this.model) {
|
||||
|
||||
var self = this;
|
||||
this.originalSync = this.model.sync;
|
||||
|
||||
|
||||
var boundHandler = errorHandler.bind(this);
|
||||
|
||||
this.model.sync = function () {
|
||||
self.$el.removeBootstrapError();
|
||||
return self.originalSync.apply(this, arguments).fail(boundHandler);
|
||||
};
|
||||
}
|
||||
|
||||
if (this.model) {
|
||||
if (originalOnRender) {
|
||||
originalOnRender.call(this);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.prototype.onBeforeClose = function () {
|
||||
|
||||
if (this.model) {
|
||||
Validation.unbind(this);
|
||||
}
|
||||
|
||||
if (originalBeforeClose) {
|
||||
originalBeforeClose.call(this);
|
||||
}
|
||||
};
|
||||
|
||||
this.prototype.onClose = function () {
|
||||
|
||||
if (this.model && this.model.isNew()) {
|
||||
this.model.destroy();
|
||||
}
|
||||
|
||||
if (originalOnClose) {
|
||||
originalBeforeClose.call(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var errorHandler = function (response) {
|
||||
|
||||
if (response.status === 400) {
|
||||
|
||||
var view = this;
|
||||
|
||||
var validationErrors = JSON.parse(response.responseText);
|
||||
|
||||
_.each(validationErrors, function (error) {
|
||||
view.$el.addBootstrapError(error);
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue