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 list editor to work with Backbone.Model #334

Open
wants to merge 2 commits 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
23 changes: 17 additions & 6 deletions distribution.amd/backbone-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2012,22 +2012,24 @@ 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');
},

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 data = this.value || {},
key = this.key,
nestedModel = this.schema.model;
key = this.key;

// TODO: This works for me as my nested models are never null but what about Backbone.Relational or other implementations?
//Wrap the data in a model if it isn't already a model instance
var modelInstance = (data.constructor === nestedModel) ? data : new nestedModel(data);
if (!(data instanceof Backbone.Model)) {
if (!this.schema.model) throw new Error('Missing required "schema.model" option for NestedModel editor');
data = new this.schema.model(data)
}

this.nestedForm = new NestedForm({
model: modelInstance,
model: data,
idPrefix: this.id + '_',
fieldTemplate: 'nestedField'
});
Expand Down Expand Up @@ -2056,8 +2058,17 @@ Form.editors.NestedModel = Form.editors.Object.extend({
}

return Form.editors.Object.prototype.commit.call(this);
}
},

getValue: function() {
if (this.nestedForm) {
// Return the value in the same way you got it
if (this.value instanceof Backbone.Model) this.nestedForm.commit();
else return this.nestedForm.getValue();
}

return this.value;
}
});

/**
Expand Down
2 changes: 1 addition & 1 deletion distribution.amd/backbone-forms.min.js

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions distribution.amd/editors/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,14 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
var self = this,
ModalForm = this.form.constructor;

var form = this.modalForm = new ModalForm({
schema: this.nestedSchema,
data: this.value
});
var formAttrs = { schema: this.nestedSchema };
if (this.value instanceof Backbone.Model) {
formAttrs.model = this.value;
}
else {
formAttrs.data = this.value;
}
var form = this.modalForm = new ModalForm(formAttrs);

var modal = this.modal = new Form.editors.List.Modal.ModalAdapter({
content: form,
Expand Down Expand Up @@ -546,7 +550,12 @@ define(['jquery', 'underscore', 'backbone', 'backbone-forms'], function($, _, Ba
if (error) return modal.preventClose();

//Store form value
this.value = form.getValue();
if (this.value instanceof Backbone.Model) {
form.commit();
}
else {
this.value = form.getValue();
}

//Render item
this.renderSummary();
Expand Down
2 changes: 1 addition & 1 deletion distribution.amd/editors/list.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions distribution/backbone-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2025,22 +2025,24 @@ 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');
},

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 data = this.value || {},
key = this.key,
nestedModel = this.schema.model;
key = this.key;

// TODO: This works for me as my nested models are never null but what about Backbone.Relational or other implementations?
//Wrap the data in a model if it isn't already a model instance
var modelInstance = (data.constructor === nestedModel) ? data : new nestedModel(data);
if (!(data instanceof Backbone.Model)) {
if (!this.schema.model) throw new Error('Missing required "schema.model" option for NestedModel editor');
data = new this.schema.model(data)
}

this.nestedForm = new NestedForm({
model: modelInstance,
model: data,
idPrefix: this.id + '_',
fieldTemplate: 'nestedField'
});
Expand Down Expand Up @@ -2069,8 +2071,17 @@ Form.editors.NestedModel = Form.editors.Object.extend({
}

return Form.editors.Object.prototype.commit.call(this);
}
},

getValue: function() {
if (this.nestedForm) {
// Return the value in the same way you got it
if (this.value instanceof Backbone.Model) this.nestedForm.commit();
else return this.nestedForm.getValue();
}

return this.value;
}
});

/**
Expand Down
2 changes: 1 addition & 1 deletion distribution/backbone-forms.min.js

Large diffs are not rendered by default.

Loading