Sha256: 289801c3f0ed946cb67255add0556d55dac675d71196321917e2bfdb2c0d1d28

Contents?: true

Size: 1.77 KB

Versions: 12

Compression:

Stored size: 1.77 KB

Contents

class ADF.Form.Models.Base extends Backbone.Model
    
  constructor: () ->
    @correct = false
    @view = null
    @fieldBinders = []
    @errors = []
    @validations = []
    @markChecks = false
    @bindFocus = true
    super()
    
  setView: (view) =>
    @view = view

  bindValue: (valueName, options = {}) ->
    options.markChecks = @markChecks
    options.bindFocus = @bindFocus
    fieldBinder = new ADF.Form.Models.FieldBinder(@, valueName, options)
    @fieldBinders.push(fieldBinder)
    fieldBinder

  # Validation
  onValueChanged: () =>
    @validateFields()
       
  validateModel: () ->
    if @isValid() && @isCustomValidationValid()
      @errors = []
      @view.onDataValid() if @view
      return true
    else
      @view.onDataInvalid() if @view
      return false
  
  validateFields: () ->
    @errors = []
    validation.validate() for validation in @validations
    @validateModel()

  isValid: () ->
    for validation in @validations
      if !validation.isValid()
        return false
    return true

  getError: (attribute) ->
    for error in @errors
      if error.attribute == attribute
        return error
    return null

  getErrorByType: (attribute, type) ->
    for error in @errors
      if error.attribute == attribute and error.type == type
        return error
    return null

  hasError: (attribute) ->
    @getError(attribute) != null

  validates: (type, attributes, options = {}) =>
    @validations.push(new ADF.Form.Models.Validator(@, attribute, type, options)) for attribute in attributes

  clearValidations: (attributes) ->
    for attribute in attributes
      @validations = _.reject @validations, (validation) => 
        return validation.getAttribute() == attribute
    @validateFields()

  # Callbacks
  isCustomValidationValid: ->
    return true

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
socmap_adf-1.0.0 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.11 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.10 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.9 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.8 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.7 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.6 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.5 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.4 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.3 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.2 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee
socmap_adf-0.0.1 lib/assets/javascripts/socmap_adf/modules/form/models/base.js.coffee