ajax.coffee | |
---|---|
Copyright 2012-2014 The Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | |
t5/core/ajaxExports a single function, that invokes
It wraps (or provides) | define ["./pageinit", "./dom", "./exception-frame", "./console", "underscore"],
(pageinit, dom, exceptionframe, console, _) ->
(url, options) ->
complete = ->
if options.complete
options.complete()
return
newOptions = _.extend {}, options, |
Logs the exception to the console before passing it to the provided exception handler or throwing the exception. | exception: (exception) ->
console.error "Request to #{url} failed with #{exception}"
if options.exception
options.exception exception
else
throw exception
complete()
return
failure: (response, failureMessage) ->
raw = response.header "X-Tapestry-ErrorMessage"
unless _.isEmpty raw
message = window.unescape raw
console.error "Request to #{url} failed with '#{message}'."
contentType = response.header "content-type"
isHTML = contentType and (contentType.split(';')[0] is "text/html")
if isHTML
exceptionframe response.text
else
console.error failureMessage
options.failure and options.failure(response)
complete()
return
success: (response) ->
pageinit.handlePartialPageRenderResponse response, options.success
complete()
return
dom.ajaxRequest url, newOptions
|