app/assets/javascripts/formagic/inputs/documents.coffee in formagic-0.1.0 vs app/assets/javascripts/formagic/inputs/documents.coffee in formagic-0.2.5
- old
+ new
@@ -26,15 +26,20 @@
class @InputForm
constructor: (@name, @nestedObjects, @config, @object) ->
@forms = []
- @config.namePrefix ||= name
- @config.removeButton = true
- @config.formSchema._id = { type: 'hidden', name: 'id' }
+ @config.namePrefix ||= name
+ @config.removeButton = if @config.disableRemoveDocuments then false else true
+ @config.ignoreOnSubmission ||= false
+
@reorderContainerClass = "nested-forms-#{@config.klassName}"
+ if @config.ignoreOnSubmission
+ for key, inputConfig of @config.formSchema
+ inputConfig.ignoreOnSubmission = true
+
@_create_el()
@_add_label()
@_add_forms()
@_add_new_button()
@@ -53,11 +58,25 @@
@$errorMessage =$ "<span class='error-message'></span>"
@$label.append(@$errorMessage)
@$el.append(@$label)
+ _extend_schema_with: (name, config) ->
+ schemaConfig = {}
+ schemaConfig[name] = config
+ @config.formSchema = $.extend(schemaConfig, @config.formSchema)
+
+
_add_forms: ->
+ # add id to schema
+ # @NOTE: here we use _id, cause mongosteen returns objects _id, but we should send id for nested documents
+ @_extend_schema_with('_id', { type: 'hidden', name: 'id', ignoreOnSubmission: @config.ignoreOnSubmission })
+
+ # add position to schema
+ if @config.sortBy
+ @_extend_schema_with(@config.sortBy, { type: 'hidden', ignoreOnSubmission: @config.ignoreOnSubmission })
+
@$forms =$ "<ul>"
@$label.after @$forms
# if not default value which means no objects
if @nestedObjects != ''
@@ -70,11 +89,10 @@
@_bind_forms_reorder()
_sort_nested_objects: ->
if @config.sortBy
- @config.formSchema[@config.sortBy] = { type: 'hidden' }
if @nestedObjects
# this is not required but make things a bit easier on the backend
# as object don't have to be in a specific order.
@nestedObjects.sort (a, b) => parseFloat(a[@config.sortBy]) - parseFloat(b[@config.sortBy])
# normalizes nested objects positions
@@ -91,16 +109,19 @@
return form
_add_new_button: ->
- label = @config.newButtonLabel || "Add"
- @$newButton =$ """<a href='#' class='nested-form-new'>#{ label }</a>"""
- @$el.append @$newButton
- @$newButton.on 'click', (e) => e.preventDefault() ; @addNewForm()
+ if ! @config.disableNewDocuments
+ label = @config.newButtonLabel || "Add"
+ @$newButton =$ """<a href='#' class='nested-form-new'>#{ label }</a>"""
+ @$el.append @$newButton
+ @$newButton.on 'click', (e) => e.preventDefault() ; @addNewForm()
+
+
# PUBLIC ================================================
initialize: ->
@config.beforeInitialize?(this)
@@ -139,15 +160,12 @@
if @config.sortBy
@_add_form_reorder_button(form)
prevForm = _last(@forms)
position = if prevForm then prevForm.inputs[@config.sortBy].value + 1 else 1
- # @TODO: having an issue here for scenario when no nested object are there for new object
- # form.inputs doesn't include sortBy field
- console.log @config
- console.log @config.sortBy
- console.log form.inputs
+ # Had a problem here for new documents, but now seems to be resolved by adding
+ # _position to schema in constructor (to be removed when stable).
form.inputs[@config.sortBy].updateValue(position)
@forms.push(form)
@@ -155,12 +173,20 @@
return form
updateValue: (@nestedObjects, @object) ->
+ # New document should update id, also after uploading images form for existing
+ # document might change, so we reset all nested forms to reflect these updates.
+ # This implementation has some problems with jumping screen when using plugins
+ # also it requires complex implementation for data local storage caching.
@$forms.remove()
@forms = []
@_add_forms()
+
+ # Initialize input plugins after update
+ for nestedForm in @forms
+ nestedForm.initializePlugins()
include(InputForm, inputFormReorder)