Sha256: 3cd3c2ba5e3280466211549f0522e3a5752db84ff7a9a3ba209ddc1e5871dcae

Contents?: true

Size: 1.55 KB

Versions: 30

Compression:

Stored size: 1.55 KB

Contents

# TODO Add base settings to validation
# model = window.model

# TODO implement method
# model[resource].validators_on 'field' # Get all validators related to this field

rest        = require '../rest'
stampit     = require '../../../vendor/stampit'

remoteable = stampit
  validate_each: (record, attribute, value) ->
    data = @json record

    # TODO best partial application
    # @post(data).done(_.partial @succeeded, record)
    @post(data).done((json) => @succeeded(json, record))

  json: (record) ->
    param = @resource.param_name || @resource.toString()

    data = {}
    data[param] = record.json()

    # TODO stop renaming id field, lol
    data[param].id ||= data[param]._id
    delete data[param]._id

    data

  # TODO Use rest.post
  post: (data) ->

    jQuery.ajax
      url     : @route
      data    : data
      type    : 'post'
      dataType: 'json'
      context : @

  succeeded: (json, record) ->
    error_messages = json[@attribute_name]

    return unless error_messages

    for error_message in error_messages
      record.errors.add @attribute_name, 'server', server_message: error_message
,
  message: "Remote validation failed"
  route: null
, ->
  # TODO discover why @model.route is coming null!
  # Desglobalize model constant
  pluralized_resource  = model.pluralize @model.resource.toString()
  @resource            = @model.resource
  @route             ||= "/#{pluralized_resource}/validate"
  @


composed = stampit.compose require('./validatorable'), remoteable
composed.definition_key = 'validates_remotely'
module.exports = composed

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
ende-0.5.22 components/indefinido/indemma/master/src/lib/record/validations/remote.coffee
ende-0.5.21 components/indefinido/indemma/master/src/lib/record/validations/remote.coffee
ende-0.4.25 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.24 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.23 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.5.1 components/indefinido/indemma/master/src/lib/record/validations/remote.coffee
ende-0.4.22 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.21 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.5.0 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.20 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.19 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.18 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.17 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.16 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.15 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.14 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.13 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.12 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.11 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee
ende-0.4.10 vendor/components/indefinido-indemma/src/lib/record/validations/remote.coffee