Fixed form initialisation with empty model/attributes.

This commit is contained in:
Martin Edenhofer 2016-02-23 11:54:35 +01:00
parent cca9740465
commit 34e92456f7
4 changed files with 28 additions and 12 deletions

View file

@ -9,6 +9,11 @@ class App.ControllerForm extends App.Controller
@handlers.push @showHideToggle @handlers.push @showHideToggle
@handlers.push @requiredMandantoryToggle @handlers.push @requiredMandantoryToggle
if !@model
@model = {}
if !@attributes
@attributes = []
# set empty class attributes if needed # set empty class attributes if needed
if !@form if !@form
@form = @formGen() @form = @formGen()
@ -16,14 +21,9 @@ class App.ControllerForm extends App.Controller
# add alert placeholder # add alert placeholder
@form.prepend('<div class="alert alert--danger js-alert hide" role="alert"></div>') @form.prepend('<div class="alert alert--danger js-alert hide" role="alert"></div>')
if !@model
@model = {}
if !@attributes
@attributes = []
# if element is given, prepend form to it # if element is given, prepend form to it
if @el if @el
@el.prepend( @form ) @el.prepend(@form)
# trigger change to rebuild shown/hidden item and update sub selections # trigger change to rebuild shown/hidden item and update sub selections
if typeof @form is 'object' if typeof @form is 'object'
@ -38,7 +38,7 @@ class App.ControllerForm extends App.Controller
@form @form
showAlert: (message) => showAlert: (message) =>
@form.find('.alert').removeClass('hide').html( App.i18n.translateContent( message ) ) @form.find('.alert').removeClass('hide').html( App.i18n.translateContent(message) )
hideAlert: => hideAlert: =>
@form.find('.alert').addClass('hide').html() @form.find('.alert').addClass('hide').html()
@ -46,7 +46,7 @@ class App.ControllerForm extends App.Controller
html: => html: =>
@form.html() @form.html()
formGen: -> formGen: =>
App.Log.debug 'ControllerForm', 'formGen', @model.configure_attributes App.Log.debug 'ControllerForm', 'formGen', @model.configure_attributes
# check if own fieldset should be generated # check if own fieldset should be generated
@ -55,12 +55,14 @@ class App.ControllerForm extends App.Controller
else else
fieldset = $('<fieldset></fieldset>') fieldset = $('<fieldset></fieldset>')
return fieldset if _.isEmpty(@model)
# collect form attributes # collect form attributes
@attributes = [] @attributes = []
if @model.attributesGet if @model.attributesGet
attributesClean = @model.attributesGet(@screen) attributesClean = @model.attributesGet(@screen)
else else
attributesClean = App.Model.attributesGet(@screen, @model.configure_attributes ) attributesClean = App.Model.attributesGet(@screen, @model.configure_attributes)
for attributeName, attribute of attributesClean for attributeName, attribute of attributesClean
@ -81,7 +83,7 @@ class App.ControllerForm extends App.Controller
attribute_count = attribute_count + 1 attribute_count = attribute_count + 1
# add item # add item
item = @formGenItem( attribute, className, fieldset, attribute_count ) item = @formGenItem(attribute, className, fieldset, attribute_count)
item.appendTo(fieldset) item.appendTo(fieldset)
# if password, add confirm password item # if password, add confirm password item
@ -96,7 +98,7 @@ class App.ControllerForm extends App.Controller
if !attribute.single if !attribute.single
attribute.display = attribute.display + ' (confirm)' attribute.display = attribute.display + ' (confirm)'
attribute.name = attribute.name + '_confirm' attribute.name = attribute.name + '_confirm'
item = @formGenItem( attribute, className, fieldset, attribute_count ) item = @formGenItem(attribute, className, fieldset, attribute_count)
item.appendTo(fieldset) item.appendTo(fieldset)
if @fullForm if @fullForm
@ -188,7 +190,7 @@ class App.ControllerForm extends App.Controller
### ###
formGenItem: (attribute_config, classname, form, attribute_count ) -> formGenItem: (attribute_config, classname, form, attribute_count) ->
attribute = clone( attribute_config, true ) attribute = clone( attribute_config, true )
# create item id # create item id

View file

@ -0,0 +1,5 @@
class AppVersionForNewMessages < ActiveRecord::Migration
def up
AppVersion.set(true)
end
end

View file

@ -27,6 +27,7 @@ set new app version and if browser reload is required
=end =end
def self.set(reload_required = false) def self.set(reload_required = false)
return false if !Setting.find_by(name: 'app_version')
version = "#{Time.zone.now.strftime('%Y%m%d%H%M%S')}:#{reload_required}" version = "#{Time.zone.now.strftime('%Y%m%d%H%M%S')}:#{reload_required}"
Setting.set('app_version', version) Setting.set('app_version', version)

View file

@ -1,5 +1,13 @@
// form // form
test( "form without @el", function() {
var form = new App.ControllerForm()
equal($(form.html()).is('div'), true)
equal($(form.html()).hasClass('alert'), true)
equal($(form.html()).hasClass('hide'), true)
})
test( "form elements check", function() { test( "form elements check", function() {
// deepEqual( item, test.value, 'group set/get tests' ); // deepEqual( item, test.value, 'group set/get tests' );
$('#forms').append('<hr><h1>form elements check</h1><form id="form1"></form>') $('#forms').append('<hr><h1>form elements check</h1><form id="form1"></form>')