-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathnestedmodel.js
More file actions
60 lines (48 loc) · 1.64 KB
/
nestedmodel.js
File metadata and controls
60 lines (48 loc) · 1.64 KB
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
/**
* NestedModel editor
*
* Creates a child form. For editing nested Backbone models
*
* Special options:
* schema.model: Embedded model constructor
*/
Form.editors.NestedModel = Form.editors.Object.extend({
initialize: function(options) {
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;
//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({
model: modelInstance,
idPrefix: modelInstance.cid + '_',
fieldTemplate: 'nestedField'
});
this._observeFormEvents();
//Render form
this.$el.html(this.nestedForm.render().el);
if (this.hasFocus) this.trigger('blur', this);
return this;
},
/**
* Update the embedded model, checking for nested validation errors and pass them up
* Then update the main model if all OK
*
* @return {Error|null} Validation error or null
*/
commit: function() {
var error = this.nestedForm.commit();
if (error) {
this.$el.addClass('error');
return error;
}
return Form.editors.Object.prototype.commit.call(this);
}
});