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
|
var SerializableView = View.extend({
events: {
"change select": "update_select",
"focus input": "focus_input",
},
preload: function(){
if (sdk.env == "production") { return }
this.test_data['ConfirmEmail'] = this.test_data['Email']
Object.keys(this.test_data).forEach(function(key){
var value = this.test_data[key]
var $el = this.$("[name=" + key + "]")
if ($el.attr("type") == "checkbox") {
$el.prop("checked", value)
}
else if ($el.prop("tagName") == "SELECT") {
$el.val( value )
this.update_select({ currentTarget: $el })
}
else {
$el.val( value )
}
}.bind(this))
},
serialize: function(){
var fields = {}
this.$("input[name], select[name], textarea[name]").each( function(){
fields[this.name] = this.value
})
return fields
},
deserialize: function(data){
this.$("input[name], textarea[name]").val("")
Object.keys(data).forEach(function(k){
this.$("[" + k + "]").val(data[k])
})
},
focus_input: function(e){
$(e.currentTarget).removeClass("error_hilite")
},
update_select: function(e){
var $target = $(e.currentTarget), value = $target.val()
var label = $($("select")[0]).find("option").filter(function(){ return this.value === value }).html()
$target.parent().addClass("picked")
$target.parent().find("span").html(label)
},
validate: function(errors){
var data = this.serialize()
var errors = []
var presence_msgs = this.validate_presence
Object.keys(presence_msgs).forEach(function(k){
if (! data[k]) errors.push( [ k, presence_msgs[k] ] )
})
this.validate_fields && this.validate_fields(data, errors)
this.cc && this.cc.validate(errors)
this.address && this.address.validate(errors)
return { errors: errors, data: data }
},
show_errors: function(errors){
var msgs = []
errors.forEach(function(e, i){
if (i > 0) { return }
this.$("[name=" + e[0] + "]").addClass('error_hilite')
msgs.push(e[1])
}.bind(this))
this.$msg.html(msgs.join("<br>"))
this.$msg.addClass('alert-notice')
},
hide_errors: function(){
this.$msg.removeClass('alert-notice')
this.$msg.html("")
},
save: function(e){
e && e.preventDefault()
var valid = this.validate()
if (valid.errors.length) {
this.show_errors(valid.errors)
return
}
else {
this.hide_errors()
}
app.curtain.show("loading")
this.action({
data: valid.data,
success: function(data){
app.curtain.hide("loading")
this.success(data)
}.bind(this),
error: function(data){
app.curtain.hide("loading")
this.error(data)
}.bind(this),
})
},
success: function(data){
console.log("SUCCESS", data)
},
error: function(data){
console.log("FAIL", data)
},
})
var FormView = View.extend(SerializableView.prototype).extend(ScrollableView.prototype)
|