Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Form.NestForm template to allow different theming for nested forms used in Object editor #392

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var fileList = [
'fieldset.js',
'field.js',
'nestedField.js',
'nestedForm.js',
'editor.js',
'editors/text.js',
'editors/textarea.js',
Expand Down
13 changes: 1 addition & 12 deletions src/editors/nestedmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ Form.editors.NestedModel = Form.editors.Object.extend({

if (!this.form) throw new Error('Missing required option "form"');
if (!options.schema.model) throw new Error('Missing required "schema.model" option for NestedModel editor');
},

render: function() {
//Get the constructor for creating the nested form; i.e. the same constructor as used by the parent form
var NestedForm = this.form.constructor;
var NestedForm = this.form.NestedForm;

var data = this.value || {},
key = this.key,
Expand All @@ -30,15 +28,6 @@ Form.editors.NestedModel = Form.editors.Object.extend({
idPrefix: this.id + '_',
fieldTemplate: 'nestedField'
});

this._observeFormEvents();

//Render form
this.$el.html(this.nestedForm.render().el);

if (this.hasFocus) this.trigger('blur', this);

return this;
},

/**
Expand Down
9 changes: 5 additions & 4 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ Form.editors.Object = Form.editors.Base.extend({
//Check required options
if (!this.form) throw new Error('Missing required option "form"');
if (!this.schema.subSchema) throw new Error("Missing required 'schema.subSchema' option for Object editor");
},

render: function() {
//Get the constructor for creating the nested form; i.e. the same constructor as used by the parent form
var NestedForm = this.form.constructor;
var NestedForm = this.form.NestedForm;

//Create the nested form
this.nestedForm = new NestedForm({
Expand All @@ -35,6 +33,9 @@ Form.editors.Object = Form.editors.Base.extend({
idPrefix: this.id + '_',
Field: NestedForm.NestedField
});
},

render: function() {

this._observeFormEvents();

Expand All @@ -54,7 +55,7 @@ Form.editors.Object = Form.editors.Base.extend({
setValue: function(value) {
this.value = value;

this.render();
this.nestedForm.setValue(this.value);
},

focus: function() {
Expand Down
1 change: 1 addition & 0 deletions src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var Form = Backbone.View.extend({
this.Fieldset = options.Fieldset || this.Fieldset || constructor.Fieldset;
this.Field = options.Field || this.Field || constructor.Field;
this.NestedField = options.NestedField || this.NestedField || constructor.NestedField;
this.NestedForm = options.NestedForm || this.NestedForm || constructor.NestedForm;

//Check which fields will be included (defaults to all)
var selectedFields = this.selectedFields = options.fields || _.keys(schema);
Expand Down
12 changes: 12 additions & 0 deletions src/nestedForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

//==================================================================================================
//NESTEDFIELD
//==================================================================================================

Form.NestedForm = Form.extend({

template: _.template('\
<span data-fieldsets></span>\
', null, Form.templateSettings)

});
3 changes: 3 additions & 0 deletions src/templates/bootstrap3.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
');

Form.editors.Base.prototype.className = 'form-control';
Form.editors.Object.prototype.className = '';
Form.editors.NestedModel.prototype.className = '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure this is correct. I don't know enough about bootstrap, or have anywhere setup to test. @fonji maybe you could confirm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm it works and is nicer than my solution.
Please ask if you want details about those lines.

I still think there's some work to do on those templates, especially for the object type, as the fields titles are hidden.
Here's a fiddle.


Form.Field.errorClassName = 'has-error';


Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script src="../src/fieldset.js"></script>
<script src="../src/field.js"></script>
<script src="../src/nestedField.js"></script>
<script src="../src/nestedForm.js"></script>
<script src="../src/editor.js"></script>

<script src="../src/editors/text.js"></script>
Expand Down