Sha256: 478dfe5d666f6c097745d4f54b0886194d3800237050bea66f23862c74d07b01

Contents?: true

Size: 1.41 KB

Versions: 15

Compression:

Stored size: 1.41 KB

Contents

class ManagedRequest
  defaults:
    type: "GET"
    url: ""
    name: "managedRequest"

  parseResponse: (jqXHR) ->
    obj = {}
    obj.responseText = jqXHR.responseText
    obj.responseStatus = jqXHR.status
    try
      obj.data= JSON.parse( jqXHR.responseText )
    catch error
      obj.data= {}
    obj

  constructor: ( options ) ->
    @options = _.extend {}, @defaults, @options, options
    @requester = Backbone.ajax
    #@requester = window.app.ajax
    @requester = @options.ajax if @options.ajax

    @trigger = () ->
      debug "NO TRIGGER"

    if @options.context and @options.context.trigger
      @trigger = @options.context.trigger
      @trigger = _.bind(@options.context.trigger,@options.context)

    _.bindAll @

  execute: ->
    @requester
      type: @options.type
      url: @options.url
      data: @options.data
      success: @success
      error: @error
      complete: @complete
      dataType: "json"
      contentType: "application/json"
      processData: false

  success: (data, textStatus, jqXHR) ->
    if data.errors
      @trigger "#{@options.name}:fail", @parseResponse( jqXHR )
    else
      @trigger "#{@options.name}:done", @parseResponse( jqXHR )
  error: (jqXHR, textStatus, errorThrown) ->
    @trigger "#{@options.name}:fail", @parseResponse( jqXHR )
  complete: (jqXHR, textStatus, errorThrown) ->
    @trigger "#{@options.name}:all", @parseResponse( jqXHR )

@ManagedRequest = ManagedRequest

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
iugu-ux-1.0.25 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.24 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.23 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.22 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.21 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.20 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.19 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.18 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.17 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.16 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.15 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.14 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.13 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.12 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.11 vendor/assets/javascripts/web-app/managed_request.coffee