forked from tobinskinner/backform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
269 lines (250 loc) · 7.67 KB
/
example.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
$(document).ready(function() {
// Mock Backbone.sync
Backbone.sync = function(method, model, options) {
var deferred = $.Deferred();
deferred.resolve(model.toJSON());
return deferred.promise();
};
// Backform example - a person model with a nested address object
var person = window.person = new Backbone.Model({
id: 101,
salutation: "Mr",
firstName: "Andre",
lastName: "Jones",
adult: true,
address: {
address1: "1751 rue Richardson",
address2: "Suite 3.105",
city: "Montréal",
postalCode: "H3K 1G6",
province: "QC"
},
dateOfBirth: "1990-10-10",
lifeGoal: "To become the best basketball player there is. I want to dunk!",
jsonValue: null
});
new Backform.Form({
el: "#form",
model: person,
fields: [
{name: "id", label: "Id", control: "uneditable-input"},
{name: "firstName", label: "First Name", control: "input"},
{name: "lastName", label: "Last Name", control: "input"},
{name: "adult", label: "Adult", control: "checkbox"},
{
name: "salutation",
label: "Salutation",
control: "radio",
options: [
{label: "Mr.", value: "Mr"},
{label: "Mrs.", value: "Mrs"},
{label: "Mme.", value: "Mme"}
]
},
{control: "spacer"},
{name: "address.address1", label: "Address1", control: "input"},
{name: "address.address2", label: "Address2", control: "input"},
{name: "address.city", label: "City", control: "input"},
{name: "address.postalCode", label: "Postal Code", control: "input"},
{
name: "address.province",
label: "Province",
control: "select",
options: [
{label: "Alberta", value: "AB"},
{label: "British Columbia", value: "BC"},
{label: "Manitoba", value: "MB"},
{label: "New Brunswick", value: "NB"},
{label: "Newfoundland and Labrador", value: "NL"},
{label: "Northwest Territories", value: "NT"},
{label: "Nova Scotia", value: "NS"},
{label: "Nunavut", value: "NU"},
{label: "Ontario", value: "ON"},
{label: "Prince Edward Island", value: "PE"},
{label: "Québec", value: "QC"},
{label: "Saskatchewan", value: "SK"},
{label: "Yukon", value: "YT"}
]
},
{name: "dateOfBirth", label: "Date of birth", control: "datepicker", options: {format: "yyyy-mm-dd"}},
{name: "lifeGoal", label: "Life goal", control: "textarea", extraClasses: ["fancy"], helpMessage: "Be creative!"},
{
name: "jsonValue",
label: "JSON value",
control: "select",
options: [
{label: "null", value: null},
{label: "true", value: true},
{label: "false", value: false},
{label: "0", value: 0},
{label: "1", value: 1},
{label: "99", value: 99},
{label: "a string", value: "a string"}
]
},
{control: "button", label: "Save to server"}
],
events: {
"submit": function(e) {
e.preventDefault();
this.model.save().done(function(result) {
console.log(result);
alert("Form saved to server!");
});
return false;
}
}
}).render();
person.on("change", function() {
$("#object").text(JSON.stringify(person.toJSON(), null, 2));
}).trigger("change");
// Example with question
window.f = new Backform.Form({
el: $("#form-question"),
model: new Backbone.Model({toggle: false, years: 0}),
fields: [{
name: "toggle",
label: "Are you a programmer?",
control: "radio",
options: [{label: "Yes", value: true}, {label: "No", value: false}]
}, {
name: "years",
label: "For how many years?",
disabled: function(m) { return m && m.get("toggle"); },
control: Backform.InputControl.extend({
initialize: function() {
Backform.InputControl.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, "change:toggle", this.render);
}
})
}]
}).render();
// Example with question (visible attribute)
new Backform.Form({
el: $("#form-visible"),
model: new Backbone.Model({toggle: false, years: 0}),
fields: [{
name: "toggle",
label: "Are you a programmer?",
control: "radio",
options: [{label: "Yes", value: true}, {label: "No", value: false}]
}, {
name: "years",
label: "For how many years?",
visible: function(m) {
return m && m.get("toggle");
},
control: Backform.InputControl.extend({
initialize: function() {
Backform.InputControl.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, "change:toggle", this.render);
}
})
}]
}).render();
// Example with input of type email
new Backform.Form({
el: $("#form-email"),
model: new Backbone.Model({email: "jonsnow@castlebla.ck", age:20}),
fields: [{
name: "email",
label: "Email",
control: "input",
type: "email",
required: true
}, {
name: "age",
label: "Age",
control: "input",
type: "number",
required: true
}, {
control: "button"
}]
}).render();
$("#form-email").on("submit", function(e) {
alert("Browser validation passed");
return false;
});
// Example with deeply nested objects
var personAndFamily = new Backbone.Model({
"firstName": "Andre",
"lastName": "Jones",
"relatives": {
"mother": {
"firstName": "Elizabeth",
"lastName": "Jones"
},
"father": {
"firstName": "Douglas",
"lastName": "Jones"
}
}
});
new Backform.Form({
el: "#form-nested",
model: personAndFamily,
fields: [
{name: "firstName", label: "First Name", control: "input"},
{name: "lastName", label: "Last Name", control: "input"},
{
name: "relatives.mother.firstName",
label: "Mother's First Name",
control: "input",
}, {
name: "relatives.mother.lastName",
label: "Mother's Last Name",
control: "input",
}, {
name: "relatives.father.firstName",
label: "Father's First Name",
control: "input",
}, {
name: "relatives.father.lastName",
label: "Father's Last Name",
control: "input",
}
]
}).render();
personAndFamily.on("change", function() {
$("#nested-object").text(JSON.stringify(personAndFamily.toJSON(), null, 2));
}).trigger("change");
// Example with validation
var MyModel = Backbone.Model.extend({
defaults: {
a: null
},
validate: function(attributes, options) {
this.errorModel.clear();
var number = parseFloat(this.get("a"), 10);
if (isNaN(number))
this.errorModel.set({a: "Not a number!"});
else if (number <= 10 || number >= 20)
this.errorModel.set({a: "Must be between 10 and 20"});
if (!_.isEmpty(_.compact(this.errorModel.toJSON())))
return "Validation errors. Please fix.";
}
});
var form = new Backform.Form({
el: "#form-validation",
model: new MyModel(),
fields: [{
name: "a",
label: "Type in a number between 10 and 20. Submit the form to validate.",
control: "input",
type: "number"
}, {
id: "submit",
control: "button"
}]
}).render();
form.$el.on("submit", function(e) {
e.preventDefault();
var submit = form.fields.get("submit");
if (form.model.isValid())
submit.set({status:"success", message: "Success!"});
else
submit.set({status:"error", message: form.model.validationError});
return false;
});
});