-
-
Save dznz/5770980 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(Form) { | |
var editors = Form.editors; | |
editors.List.InlineNestedModel = editors.List.NestedModel.extend({ | |
events: {}, | |
/** | |
* @param {Object} options | |
*/ | |
initialize: function(options) { | |
editors.Base.prototype.initialize.call(this, options); | |
// Reverse the effect of the "feature" of pressing enter adding new item | |
// https://github.com/powmedia/backbone-forms/commit/6201a6f44984087b71c216dd637b280dab9b757d | |
delete this.options.item.events['keydown input[type=text]']; | |
var schema = this.schema; | |
//Get nested schema if Object | |
if (schema.itemType === 'Object') { | |
if (!schema.subSchema) throw 'Missing required option "schema.subSchema"'; | |
this.nestedSchema = schema.subSchema; | |
} | |
var list = options.list; | |
list.on('add', this.onUserAdd, this); | |
}, | |
/** | |
* Render the list item representation | |
*/ | |
render: function() { | |
var self = this; | |
this.$el.html(this.getFormEl()); | |
setTimeout(function() { | |
self.trigger('readyToAdd'); | |
}, 0); | |
return this; | |
}, | |
getFormEl: function() { | |
var schema = this.schema, | |
value = this.getValue(); | |
this.form = new Form({ | |
schema: this.schema.subSchema, | |
data: this.value, | |
template: 'nestedForm' | |
//model: value | |
}); | |
return this.form.render().el; | |
}, | |
getValue: function() { | |
if (this.form) { | |
this.value = this.form.getValue(); | |
//console.log('nested form value', this.value); | |
// see https://github.com/powmedia/backbone-forms/issues/81 | |
} | |
return this.value; | |
}, | |
onUserAdd: function() { | |
this.form.$('input, textarea, select').first().focus(); | |
}, | |
}, { | |
template: _.template('<div class="bbf-nested-form"><%= fieldsets %></div>', null, Form.templateSettings) | |
}); | |
})(Backbone.Form); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment