Sha256: 01d79e989bbed418fa3733c00727e0e3d43df7771dd012bf738746afa0e619ff

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

class @FormAjax extends Backbone.View

  submitHandler : () ->
    @_prepareForAjax()
    $.ajax(
      url : @$el.attr('action')
      type : @$el.attr('method')
      data : @_getData()
      dataType : 'json'
    ).done(@_onDone).fail(@_onFail).always(@_onAlways)

  _prepareForAjax : () ->
    @trigger 'ajax-start'
    for instance of CKEDITOR.instances
      CKEDITOR.instances[instance].updateElement()

  _onDone : (data) =>
    @trigger 'ajax-done', data
    @$el.find('.error-message').remove()

  _onFail : (response) =>
    try
      @trigger 'ajax-fail', JSON.parse(response.responseText)

  _onAlways : (response) =>
    data = if (response.responseJSON) then response.responseJSON else response
    window.notifier.notify(data.message) if data.message
    @trigger 'ajax-finish', data

  _getData : () ->
    items = @$el.serializeObject()

    for memb of items.data
      array = items.data[memb]
      items.data[memb] = @_cleanArray(array) if $.isArray(array)

    inputMethod = @$el.find('input[name="_method"]')
    items['_method'] = inputMethod.val() if inputMethod instanceof jQuery

    items

  _cleanArray : (array) ->
    newArray = []
    for val in array
      cleanVal = val.replace(/\s+/g, '')
      if cleanVal.length > 0
        newArray = $.merge(newArray, cleanVal.split(','))
    if newArray.length is 0 then [''] else newArray

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rademade_admin-0.2.2 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.2.1 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.2.0 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.6 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.5 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.4 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.3 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.2 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.1 app/assets/javascripts/rademade_admin/form/form-ajax.coffee
rademade_admin-0.1.0 app/assets/javascripts/rademade_admin/form/form-ajax.coffee