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