Skip to content

Instantly share code, notes, and snippets.

@osiro
Created November 27, 2014 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osiro/a774af055bf65ffce390 to your computer and use it in GitHub Desktop.
Save osiro/a774af055bf65ffce390 to your computer and use it in GitHub Desktop.
class Tongln.AjaxRequester
constructor: (options) ->
@options = options
send: ->
$.ajax(@options)
.done (data, textStatus, jqXHR) =>
Tongln.DebugHelpers.logSuccessAjax(data, textStatus, jqXHR)
.error (jqXHR, textStatus, errorThrown) =>
Tongln.DebugHelpers.logFailAjax(jqXHR, textStatus, errorThrown)
class Tongln.DebugHelpers
@enable = false
@railsEnv: ->
"<%= Rails.env %>"
@envIsTest: ->
@railsEnv() == 'test'
@envIsDevelopment: ->
@railsEnv() == 'development'
@envIsStaging: ->
@railsEnv() == 'staging'
@envIsProduction: ->
@railsEnv() == 'production'
@isEnabled: ->
# Is always enabled by default on development and test env.
@envIsDevelopment() or @enable
@logSuccessAjax: (data, textStatus, jqXHR) ->
if console? and @isEnabled()
console.info("Ajax request has succeeded:")
console.table(data)
console.info("Status: #{jqXHR.status}")
console.info("Response body: \n #{jqXHR.responseText}")
@logFailAjax: (jqXHR, textStatus, errorThrown) ->
if console? and @isEnabled()
console.warn("Ajax request has failed:")
console.error(errorThrown)
console.error("Status: #{jqXHR.status}")
console.log("Response Body: #{jqXHR.responseText}")
class Tongln.Brownify
makeItBrown: (event) =>
# ... heaps of code before
options =
url: "http://devicebondage.com/videos/all"
type: "GET"
dataType: "json"
requester = new Tongln.AjaxRequester(options)
requester.send()
.done (data, textStatus, jqXHR) =>
@beHappy(data)
.error (jqXHR, textStatus, errorThrown) =>
alert("An unhandled server error has occurred. If you continue to see this message, please contact COMPANY support. Thanks for your patience.")
.always =>
@never()
@osiro
Copy link
Author

osiro commented Nov 27, 2014

Success

a

Error

a

@osiro
Copy link
Author

osiro commented Nov 27, 2014

You might want to replace .error by .fail and .done by .success.

I've read that .done and .error are deprecated....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment