sonarr-repo-only/UI/Mixins/AsValidatedView.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

define(
[
'backbone.validation',
'underscore',
2013-08-22 00:36:35 +00:00
'jQuery/jquery.validation'
], function (Validation, _) {
'use strict';
return function () {
var originalOnRender = this.prototype.onRender;
var originalOnClose = this.prototype.onClose;
var originalBeforeClose = this.prototype.onBeforeClose;
var errorHandler = function (response) {
if (response.status === 400) {
var view = this;
var validationErrors = JSON.parse(response.responseText);
_.each(validationErrors, function (error) {
view.$el.processServerError(error);
});
}
};
var validatedSync = function (method, model,options) {
this.$el.removeAllErrors();
arguments[2].isValidatedCall = true;
return model._originalSync.apply(this, arguments).fail(errorHandler.bind(this));
};
var bindToModel = function (model) {
if (!model._originalSync) {
model._originalSync = model.sync;
model.sync = validatedSync.bind(this);
}
};
this.prototype.onRender = function () {
2013-08-22 00:36:35 +00:00
Validation.bind(this);
this.bindToModelValidation = bindToModel.bind(this);
2013-08-22 00:36:35 +00:00
if (this.model) {
this.bindToModelValidation(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);
}
};
2013-08-22 00:36:35 +00:00
return this;
};
});