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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
var FormView = View.extend({
method: "post",
useMinotaur: false,
events: {
"submit form": "save"
},
initialize: function(opt){
if (opt && opt.parent) {
this.parent = opt.parent
}
this.$form = this.$("form")
this.$errors = this.$(".errors")
this.$errorList = this.$(".errorList")
},
reset: function(){
this.$("input,textarea").not("[type='submit']").not("[type='hidden']").val("")
},
showErrors: function(errors){
if (errors && errors.length) {
this.$errorList.empty();
for (var i in errors) {
this.$errorList.append('<div>' + errors[i] + '</div>');
}
this.$errors.css("opacity", 1.0);
setTimeout(function(){
this.$errors.show().css("opacity", 1.0);
}.bind(this), 200)
}
},
serialize: function(){
var fd = new FormData(), hasCSRF = false
this.$("input[name], select[name], textarea[name]").each( function(){
if (this.type == "file") {
if (this.files.length > 0) {
fd.append(this.name, this.files[0]);
}
}
else if (this.type == "password") {
if (this.value.length > 0) {
fd.append(this.name, SHA1.hex('bucky$' + this.value + '$bucky'))
}
}
else {
fd.append(this.name, this.value);
hasCSRF = hasCSRF || this.name == "_csrf"
}
});
// if (! hasCSRF) {
// fd.append("_csrf", $("[name=_csrf]").attr("value"))
// }
//
return fd
},
save: function(e, successCallback, errorCallback){
e && e.preventDefault()
this.$errors.hide().css("opacity", 0.0);
if (this.validate) {
var errors = this.validate()
if (errors && errors.length) {
if (errorCallback) {
errorCallback(errors)
}
else {
this.showErrors(errors)
}
return
}
}
var action = typeof this.action == "function" ? this.action() : this.action
if (! action) return
var request = $.ajax({
url: action,
type: this.method,
data: this.serialize(),
headers: { "csrf-token": $("[name=_csrf]").attr("value") },
dataType: "json",
processData: false,
contentType: false,
success: function(response){
if (response.error) {
var errors = []
for (var key in response.error.errors) {
errors.push(response.error.errors[key].message);
}
if (errorCallback) {
errorCallback(errors)
}
else {
this.showErrors(errors)
}
return
}
else {
if (successCallback) {
successCallback(response)
}
if (this.success) {
this.success(response)
}
}
}.bind(this),
error: function(response){
}.bind(this),
complete: function(response){
if (this.useMinotaur) {
Minotaur.hide()
}
}.bind(this),
})
if (this.useMinotaur) {
Minotaur.show()
}
},
})
/*
var ModalFormView = ModalView.extend(FormView.prototype).extend({
load: function(){
this.reset()
this.show()
}
})
*/
|