Created
November 27, 2014 03:42
-
-
Save osiro/a774af055bf65ffce390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
Success
Error