 #require ST/ModelFieldView
 
 ST.class 'FormView', 'View', ->
-  @initializer 'withModelAttributes', (model, attributes) ->
-    @init()
-    @_model = model
-    @_attributes = attributes
-    @_command = 'Save Form'
-    @_fields = {}
-  
-  @initializer 'withScopeAttributes', (scope, attributes) ->
-    @initWithModelAttributes scope.model(), attributes
-    @_scope = scope
-  
-  @initializer 'withItemAttributes', (item, attributes) ->
-    @initWithModelAttributes item._class, attributes
-    @_item = item
+  @initializer (options, definition) ->
+    self = this
+    
+    @super()
+    
+    @_command   = options.command || 'Save Form'
+    @_defaults  = options.default || null
+    @_fields    = ST.List.create()
+    
+    if options.scope
+      @_scope = options.scope
+      @_model = options.scope.model()
+    else if options.item
+      @_item  = options.item
+      @_model = options.item._class
+    else
+      @_model   = options.model
+    
+    dsl = {
+      _add: (field) ->
+        field.id attribute
+        field.bind 'submit', self
+        self._fields.add field
+        field.release()        
+      text: (attribute) ->
+        @_add ST.TextFieldView.create()
+      enum: (attribute) ->
+        details = self.detailsFor attribute
+        @_add ST.EnumFieldView.createWithValuesNull details.values, details.null
+      model: (attribute) ->
+        details = self.detailsFor attribute
+        field = ST.ModelFieldView.createWithModel self._model._namespace.class(details.model)
+        field.searchRemotelyAt details.searchesRemotelyAt if details.searchesRemotelyAt
+        @_add field
+    }
+    definition.call dsl
+    @loadFieldValues()
 
   @property 'defaults'
   @property 'model'

  @property 'command'
  
  @destructor ->
    for attribute, field of @_fields
      if @_fields.hasOwnProperty attribute
        field.release()
    @super()
  
  @method 'render', ->
    html = ['<table class="formView">']
    for attribute in @_attributes
      html.push '<tr><th class="label"><label for="', attribute, '">', @_model.labelForAttribute(attribute), ':</label></th><td class="field" data-attribute="', attribute, '"></td></tr>'
    html.push '</table>'
    
    @_element.html html.join('')
    
    cells = $ 'td.field', @_element
    for cell in cells
      attribute = $(cell).attr 'data-attribute'
      details = @_model._attributes[attribute]
      field = switch details.type
        when 'belongsTo'
          ST.ModelFieldView.createWithModel @_model._namespace.class(details.model)
        when 'enum'
          ST.EnumFieldView.createWithValuesNull details.values, details.null
        else
          ST.TextFieldView.create()
      field.id attribute
      field.bind 'submit', this
      field.searchRemotelyAt details.searchesRemotelyAt if details.searchesRemotelyAt
      @_fields[attribute] = field
      field.value(
        if @_item
          @_item[attribute]()
        else if @_defaults && @_defaults[attribute]
          @_defaults[attribute]
        else
          details.default
      )
      field.load()
      $(cell).append field.element()
  
  @method 'data', ->
    data = {}

        data[attribute] = @_defaults[attribute]
    
    # Read field values into data
    @_fields.each (field) ->
      data[field.id()] = field.value()
    
    data
  
