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

Allow NestedModel and Object editors' form options to be extended/overridden. #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/editors/nestedmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Form.editors.NestedModel = Form.editors.Object.extend({
Form.editors.Base.prototype.initialize.call(this, options);

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');
if (!this.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;
//Get the constructor for creating the nested form; by default the same constructor as used by the parent form
var NestedForm = this.schema.formClass || this.form.constructor;

var data = this.value || {},
key = this.key,
Expand All @@ -25,11 +25,13 @@ Form.editors.NestedModel = Form.editors.Object.extend({
//Wrap the data in a model if it isn't already a model instance
var modelInstance = (data.constructor === nestedModel) ? data : new nestedModel(data);

this.nestedForm = new NestedForm({
var formOptions = _.extend({
model: modelInstance,
idPrefix: this.id + '_',
fieldTemplate: 'nestedField'
});
Field: NestedForm.NestedField
}, _.result(this.schema, 'formOptions') || {});

this.nestedForm = new NestedForm(formOptions);

this._observeFormEvents();

Expand Down
9 changes: 5 additions & 4 deletions src/editors/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ Form.editors.Object = Form.editors.Base.extend({

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.schema.formClass || this.form.constructor;

//Create the nested form
this.nestedForm = new NestedForm({
var formOptions = _.extend({
schema: this.schema.subSchema,
data: this.value,
idPrefix: this.id + '_',
Field: NestedForm.NestedField
});
}, _.result(this.schema, 'formOptions') || {});

this.nestedForm = new NestedForm(formOptions);

this._observeFormEvents();

Expand Down