Sha256: ae6a6887bcdae44c8fea092fa1aedccb4954f42d4724ff2199d71453d4b78561

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 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 = @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: ->
    app.ajaxSetup
      headers:
        Authorization: $.base64.encode api_token

    @requester
      type: @options.type
      url: @options.url
      data: @options.data
      success: @success
      error: @error
      complete: @complete

  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

20 entries across 20 versions & 1 rubygems

Version Path
iugu-ux-1.0.10 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.9 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.8 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.7 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.6 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.5 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.4 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.3 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.2 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.1 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-1.0.0 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.9 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.8 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.5 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.4 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.3 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.2 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.1 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.9.0 vendor/assets/javascripts/web-app/managed_request.coffee
iugu-ux-0.8.9 vendor/assets/javascripts/web-app/managed_request.coffee