assets/scripts/coffee/hoarder/form/form.coffee in hoarder-js-0.0.1 vs assets/scripts/coffee/hoarder/form/form.coffee in hoarder-js-0.0.2
- old
+ new
@@ -1,48 +1,51 @@
-require "lib/jquery"
+FormSerializer = require 'hoarder/form/form_serializer'
-FormElement = require "hoarder/form/form_element"
-
-#
-# @author - Tim Shelburne <tim@musiconelive.com>
-#
-# wraps an HTML form for easier form management
-#
class Form
- constructor: (formId, @type = "simple")->
- @form = $("##{formId}")
- @form.submit (e)-> e.preventDefault()
- @action = @form.attr("action")
- @method = @form.attr("method")
- @addedElements = []
- elements: ->
- elements = []
- inputs = @form.find("input")
- elements = elements.concat inputs.toArray() if inputs.length > 0
- selects = @form.find("select")
- elements = elements.concat selects.toArray() if selects.length > 0
- textareas = @form.find("textarea")
- elements = elements.concat textareas.toArray() if textareas.length > 0
+ constructor: (@formElement)->
+ @addedElements = [ ]
- formElements = []
- for element in elements
- validationRules = if element.getAttribute("data-validation")? then (rule.trim() for rule in element.getAttribute("data-validation").split(',')) else []
- selector = "#{element.nodeName}[data-bind='#{element.getAttribute("data-bind")}']"
- formElements.push new FormElement(element.name, element.value, selector, validationRules)
- formElements
+ elements: -> (@formElement[index] for index in [0..@formElement.length] when @formElement[index]?.nodeName in [ 'INPUT', 'SELECT', 'TEXTAREA' ])
- addElement: (name, value, isRemovable = true)->
- @form.append "<input type='hidden' name='#{name}' value='#{value}'/>"
- @addedElements.push $("input[name='#{name}']", @form) if isRemovable
+ action: -> @formElement.action
- addElements: (elements)-> @addElement(element.name, element.value) for element in elements
+ method: -> @formElement.method
- updateAddedElement: (name, value)-> @form.find("input[name='#{name}']").val(value)
+ addElement: (name, value)->
+ throw new Error "'#{name}' already exists as an element on the form." if @hasElement name
+ element = createElement name, value
+ @formElement.appendChild element
+ @addedElements.push element
+ element
- clearAddedElements: ->
- element.remove() for element in @addedElements
- @addedElements = []
+ addElements: (elements)->
+ errors = [ ]
+ for element in elements
+ try @addElement(element.name, element.value)
+ catch e
+ errors.push e
+ throw errors[0] if errors.length
- serialize: -> @form.serialize()
+ hasElement: (name)-> @getElement(name)?
+
+ getElement: (name)-> @formElement[name] if @formElement[name] instanceof HTMLElement
+
+ updateAddedElement: (name, value)-> if @formElement[name]? then @formElement[name].value = value else @addElement name, value
+
+ clearAddedElements: ->
+ @formElement.removeChild element for element in @addedElements
+ @addedElements = []
+
+ serialize: -> FormSerializer.toString @
+
+
+ # private
+
+ createElement = (name, value)->
+ element = document.createElement("input")
+ element.type = "hidden"
+ element.name = name
+ element.value = value
+ element
return Form
\ No newline at end of file