-
Notifications
You must be signed in to change notification settings - Fork 12
/
ajaxform.js
123 lines (114 loc) · 3.81 KB
/
ajaxform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// AjaxForm jQuery Plugin v0.1.3
// http://github.com/alchapone/ajaxform
//
// Copyright 2012, Alex Chaplinsky
// Released under the MIT License
//
// Generated by CoffeeScript 1.3.3
(function() {
(function($) {
var AjaxForm;
$.fn.ajaxForm = function(options) {
var settings;
if (options == null) {
options = {};
}
settings = {
errorClass: 'error-field',
showErrorMessage: true,
errorMessageFormat: '<div class="error-message">{message}</div>',
insertMessage: 'before',
onRequestStart: function() {},
onRequestEnd: function() {},
onSuccess: function() {},
onErrors: function() {},
onError: function() {}
};
if ($.type(options) === 'object') {
settings = $.extend(settings, options);
if (!(['after', 'before'].indexOf(settings.insertMessage) > -1)) {
settings.insertMessage = 'before';
}
} else if ($.type(options) === 'function') {
settings.onSuccess = function(data) {
return options(data);
};
}
return $(this).each(function() {
return new AjaxForm(this, settings);
});
};
return AjaxForm = (function() {
function AjaxForm(element, settings) {
var _this = this;
this.el = element;
this.settings = settings;
this.method = $(this.el).find('[name=_method]').val() || $(this.el).attr('method') || 'post';
this.url = $(this.el).attr('action');
$(element).on('submit', function(event) {
event.preventDefault();
return _this.performRequest();
});
}
AjaxForm.prototype.performRequest = function() {
var data,
_this = this;
this.settings.onRequestStart();
data = $(this.el).serialize();
return $.ajax({
type: this.settings.method || this.method,
url: this.url,
data: data,
success: function(json) {
_this.settings.onRequestEnd();
if (json.errors === void 0) {
if (json.redirect !== void 0) {
window.location = json.redirect;
}
return _this.settings.onSuccess(json);
} else {
_this.settings.onErrors(json);
return _this.applyErrors(json.errors);
}
},
error: function(xhr) {
_this.settings.onRequestEnd();
return _this.settings.onError(xhr);
}
});
};
AjaxForm.prototype.applyErrors = function(errors) {
var _this = this;
this.clearErrors();
return $.each(errors, function(key, val) {
var field, value;
value = $.isArray(val) ? val[0] : val;
if ($(_this.el).find("[validate*=" + key + "]").length === 0) {
field = $(_this.el).find("[name*=" + key + "]");
} else {
field = $(_this.el).find("[validate*=" + key + "]");
}
return _this.addError(field, value);
});
};
AjaxForm.prototype.addError = function(field, message) {
var error;
field.addClass(this.settings.errorClass);
if (this.settings.showErrorMessage === true) {
error = this.settings.errorMessageFormat.replace('{message}', message);
return field[this.settings.insertMessage](error);
}
};
AjaxForm.prototype.clearErrors = function() {
var fields, method;
fields = $(this.el).find("." + this.settings.errorClass);
fields.removeClass(this.settings.errorClass);
if (this.settings.showErrorMessage === true) {
method = this.settings.insertMessage === 'before' ? 'prev' : 'next';
return fields[method]().remove();
}
};
return AjaxForm;
})();
})(jQuery);
}).call(this);