User form example
HTML
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="https://rawgithub.com/powmedia/backbone-forms/master/distribution/backbone-forms.js"></script>
<script src="https://rawgithub.com/powmedia/backbone-forms/master/distribution/adapters/backbone.bootstrap-modal.min.js"></script>
<script src="https://rawgithub.com/exussum12/backbone-forms/master/distribution/editors/list.js"></script>
<script id="formTemplate" type="text/html">
<form>
<div class="bounding">
<h2>Property Details</h2>
<div data-fields="propertyType,sleeps"></div>
<div data-fields="bedrooms"></div>
</div>
<div class="bounding">
<h2>Description</h2>
<div data-fields="Description"></div>
</div>
</form>
</script>
CSS
/* STYLES FROM BACKBONE-FORMS DEFAULT TEMPLATE */
/* Form */
.bbf-form {
margin: 0;
padding: 0;
border: none;
}
/* Field */
.bbf-field {
margin: 1em 0;
list-style-type: none;
position: relative;
clear: both;
}
.bbf-field label {
float: left;
width: 25%;
}
.bbf-field .bbf-editor {
margin-left: 25%;
width: 74%;
}
.bbf-field input, .bbf-field textarea, .bbf-field select {
width: 100%;
}
.bbf-field .bbf-help {
margin-left: 25%;
width: 74%;
color: #999;
}
.bbf-field.bbf-error .bbf-editor {
outline: 1px solid red;
}
/* Radio */
.bbf-radio {
list-style-type: none;
}
.bbf-radio input {
width: auto;
}
.bbf-radio label {
float: none;
}
/* Checkboxes */
.bbf-checkboxes {
list-style-type: none;
}
.bbf-checkboxes input {
width: auto;
}
.bbf-checkboxes label {
float: none;
}
/* List */
.bbf-list ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.bbf-list .bbf-error {
border: 1px solid red;
}
.bbf-list li {
clear: both;
width: 100%;
}
.bbf-list li .bbf-editor-container {
margin-right: 2em;
}
.bbf-list li .bbf-remove {
float: right;
width: 2em;
}
.bbf-list .bbf-actions {
text-align: center;
clear: both;
}
/* List.Modal */
.bbf-list-modal {
cursor: pointer;
border: 1px solid #ccc;
width: 208px;
border-radius: 3px;
padding: 4px;
color: #555;
}
/* Date */
.bbf-date .bbf-date {
width: 4em;
}
.bbf-date .bbf-month {
width: 9em;
}
.bbf-date .bbf-year {
width: 6em;
}
/* DateTime */
.bbf-datetime .bbf-date-container {
float: left;
margin-right: 1em;
}
.bbf-datetime select {
width: 4em;
}
.subclass > form > fieldset > div {
float:left;
width:20%;
min-width:150px
}
JavaScript
$(function () {
Backbone.Form.editors.List.Modal.ModalAdapter = Backbone.BootstrapModal;
var Address = Backbone.Model.extend({
schema: {
doubleBeds: {
type: 'Select',
options: [1, 2, 3, 4, 5]
},
singleBeds: {
type: 'Select',
options: [1, 2, 3, 4, 5]
},
bunkbeds: {
type: 'Select',
options: [1, 2, 3, 4, 5]
},
sofaBeds: {
type: 'Select',
options: [1, 2, 3, 4, 5]
},
enSuite: {
type: 'Checkbox',
fieldClass: "check"
}
}
});
var User = Backbone.Model.extend({
schema: {
propertyType: {
type: 'Select',
options: ['Cottage', 'Villa', 'Log Cabin', 'Apartment']
},
sleeps: {
type: 'Select',
options: [1, 2, 3, 4, 5]
},
bedrooms: {
type: 'List',
itemType: 'NestedModel',
model: Address,
amount: 5,
editorAttrs: {
class: 'subclass bedrooms'
}
},
Description: {
type: 'TextArea',
editorAttrs: {
maxlength: 5000,
title: 'Tooltip help'
}
},
}
});
var user = new User({
});
var form = new Backbone.Form({
model: user,
// template: _.template($('#formTemplate').html()),
}).render();
$('body').append(form.el);
});